3

我想返回一个std::function其类型取决于我的函数模板的一个模板参数的类型。

// 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);

逻辑上--what-goes-here?--应该是函数的函数签名,该函数返回返回类型F并采用类型参数,F但我似乎无法告诉我的编译器(Visual Studio 2010 Express)这个意图。(注意:在使用示例中,它将是std::function<bool(F)>。)

(注意:我尝试过变体std::result_of<F>::type但没有成功。)

这对 C++0x 可行吗?

4

1 回答 1

3

以下为我编译 GCC 4.5.3 和 MSVC 2010 EE SP1

auto make_f2_call(Arg1&& arg1, Arg2&& arg2)
    -> std::function< typename std::result_of<F(Arg1, Arg2)>::type (F)>
{
于 2011-08-16T15:04:20.020 回答