Menu

#59 support C++11-like auto functions

1.1.0
accepted
None
Functionality
minor
0.4.0
enhancement
2012-09-10
2012-06-07
No

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.

Discussion

  • Lorenzo Caminiti

    • status changed from new to accepted
     
  • Lorenzo Caminiti

    It could be done like this:

    #include <boost/typeof/typeof.hpp>
    #include <iostream>
    
    /*
    CONTRACT_FUNCTION( // f
        template( typename Left, typename Right )
        auto (adding_func) ( (Left const&) left, (Right const&) right )
            return(BOOT_TYPEOF_TPL(left + right))
    ) {
        return left + right;
    }
    */
    
    template< typename Left, typename Right >   // TPARAMS(f)
    struct adding_func_result_123
    {
        Left const& left;                       // PARAMS(f)
        Right const& right;                     // PARAMS(f)
        typedef
            BOOST_TYPEOF_TPL(left + right)      // RESULT_TYPE(f)
            type
        ;
    };
    
    template< typename Left, typename Right >
    typename adding_func_result_123<Left, Right>::type  // TPARAMS(f)
    adding_func ( Left const& left, Right const& right )
    {
        return left + right;
    }
    
    int main ( void )
    {
        std::cout << adding_func(1, 2) << std::endl;
        return 0;
    }
    
     
  • Lorenzo Caminiti

    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:

    // auto swap(B& x, B& y) RETURNS(swap(x.a,y.a), swap(x.b,y.b), ...);
    
    struct B { A a, b; }
    
    // Defines swap to return `return ...` and automatically figures out noexcept (on C++11).
    CONTRACT_FUNCTION_DEF(
        auto (swap) ( (B&) x, (B&) y )
            return(swap(x.a, y.a), swap(x.b, y.b))
    )
    
     
  • Lorenzo Caminiti

    • milestone changed from Future to 1.1.0
     

Log in to post a comment.