support C++11-like auto functions
Contract Programming Library for C++
Status: Pre-Alpha
Brought to you by:
lcaminiti
In C++11 the result type can be automatically deduced using the following syntax:
template< typename Left, typename Right >
auto adding_func ( Left const& left, Right const& right )
-> decltype(left + right)
{
return left + right;
}
The auto keyword is used because the decltype needs to be used after the parameter declarations so the parameter names are known.
I can support this functionality for C++03 and use BOOST_TYPEOF with the following syntax:
CONTRACT_FUNCTION(
template( typename Left, typename Right )
auto (adding_func) ( (Left const&) left, (Right const&) right )
return(BOOST_TYPEOF(left + right))
) {
return left + right;
}
return(result_type) is used instead of C++11 -> in the function declaration and after the function parameters (where exactly, after cv-qualifiers? after named parameter concepts? etc).
I can implement this in C++03 doing something similar to Boost.AutoFunction (proposed library):
https://svn.boost.org/svn/boost/sandbox/auto_function/
and or Boost.ResultOf.
It could be done like this:
From Dave:
auto functions arelly are useful, but mostly they're useful with a macro
that calculates noexcept, return type, etc.:
http://lists.boost.org/Archives/boost/2012/04/191926.php
From me:
I see but that only applies to C++11 and not to auto-functions emulated in C++03 (because there's no noexcept in C++03). Also, these type of macros only apply to 1-liner functions (of course, as you show using operator, you can concatenate a bunch of expressions on a single line).
Contract++ could support something like that: