Martin Ba
Martin Ba

Reputation: 38981

How to return a function type dependent on a template argument?

I would like to return a std::function whose type is dependent on the type of one template argument of my function template.

// Return a function object whose type is directly dependent on F
template<typename F, typename Arg1, typename Arg2>
auto make_f2_call(Arg1&& arg1, Arg2&& arg2)
    -> std::function<--what-goes-here?-->
{
    return [arg1, arg2](F f) { return f(arg1, arg2); };
}

// Usage example, so that it's clearer what the function does:
...
typedef bool (*MyFPtrT)(long id, std::string const& name);
bool testfn1(long id, std::string const& name);
...
auto c2 = make_f2_call<MyFPtrT>(i, n); // std::function<bool(F)>
...
bool result = c2(&testfn1);

Logically --what-goes-here?-- should be the function signature of a function returning the return type of F and taking an argument of type F but I seem to be unable to tell my compiler (Visual Studio 2010 Express) this intent. (Take Note: In the usage example, it would be std::function<bool(F)>.)

(Note: I have tried variations of std::result_of<F>::type without success.)

Is this possible with C++0x?

Upvotes: 3

Views: 215

Answers (1)

Cubbi
Cubbi

Reputation: 47498

The following compiles for me with both GCC 4.5.3 and MSVC 2010 EE SP1

auto make_f2_call(Arg1&& arg1, Arg2&& arg2)
    -> std::function< typename std::result_of<F(Arg1, Arg2)>::type (F)>
{

Upvotes: 3

Related Questions