Boost logo

Boost :

Subject: Re: [boost] Discovering C++11 lambda argument and result types to construct boost::function
From: Marco Cecchetti (mrcekets_at_[hidden])
Date: 2012-02-13 17:12:24


On Mon, 13 Feb 2012 20:08:34 +0100, Frank Birbacher
<bloodymir.crap_at_[hidden]> wrote:

> So this is the point: I need a signature from a member function. There
> is no type trait that does this. Any other suggestions?

The code example below compiles and runs nicely with gcc 4.7.

A note: if you substitute
     using signature = R (Arg...);
with:
     typedef R (Arg...) signature;
you get a compile time error.

Cheers,
-- Marco

#include <iostream>
#include <utility>
#include <boost/function.hpp>

using boost::function;

//////////////////////////////////////////////////////////////
// member function traits

template<typename MembFuncPtr>
struct memb_func_traits
{};

template< typename ClassT, typename R, typename... Arg>
struct memb_func_traits<R ClassT::* (Arg...)>
{
     using class_type = ClassT;
     using signature = R (Arg...);
};

template< typename ClassT, typename R, typename... Arg>
struct memb_func_traits<R (ClassT::*) (Arg...) const>
{
     using class_type = ClassT;
     using signature = R (Arg...);
};

template<typename F>
struct function_type {
     typedef typename memb_func_traits<
                 decltype(&F::operator())
>::signature signature;

     typedef function<signature> type;
};

template<typename F>
typename function_type<F>::type
make_function(F && f)
{
     typedef typename function_type<F>::type result_type;
     return result_type(std::forward<F>(f));
}

int main()
{
     auto a = make_function([](int){ return 4; });
     assert( a(1) == 4 );
     std::cout << a(1) << std::endl;
}

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk