[contract] C++11-like auto-functions

Hello all, I'm thinking to expand Boost.Contract to support C++11-like auto-functions for C++03. The following macro: 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; } Would expand to something like the code below (but the main of course): #include <boost/typeof/typeof.hpp> #include <iostream> 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; } I got a ticket for it: https://sourceforge.net/apps/trac/contractpp/ticket/59 But I might implement it only after the Boost review... (Are C++11 auto-function really useful?) Thanks. --Lorenzo

on Wed Jun 27 2012, Lorenzo Caminiti <lorcaminiti-AT-gmail.com> wrote:
Hello all,
I'm thinking to expand Boost.Contract to support C++11-like auto-functions for C++03.
I got a ticket for it: https://sourceforge.net/apps/trac/contractpp/ticket/59 But I might implement it only after the Boost review... (Are C++11 auto-function really useful?)
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 -- Dave Abrahams BoostPro Computing http://www.boostpro.com

Dave Abrahams wrote
on Wed Jun 27 2012, Lorenzo Caminiti <lorcaminiti-AT-gmail.com> wrote:
Hello all,
I'm thinking to expand Boost.Contract to support C++11-like auto-functions for C++03.
I got a ticket for it: https://sourceforge.net/apps/trac/contractpp/ticket/59 But I might implement it only after the Boost review... (Are C++11 auto-function really useful?)
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
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)) ) I'll think more about this when/if I implement auto-functions support for Contract++. --Lorenzo -- View this message in context: http://boost.2283326.n4.nabble.com/contract-C-11-like-auto-functions-tp46321... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (3)
-
Dave Abrahams
-
lcaminiti
-
Lorenzo Caminiti