如何返回依赖于模板参数的函数类型?
我想返回一个 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< 类型参数的函数的函数签名/code> 但我似乎无法告诉我的编译器(Visual Studio 2010 Express)这个意图。 (注意:在使用示例中,它将是
std::function
。)
(注意:我尝试过 的变体std::result_of
没有成功。)
这对于 C++0x 可能吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下内容为我使用 GCC 4.5.3 和 MSVC 2010 EE SP1 进行编译
The following compiles for me with both GCC 4.5.3 and MSVC 2010 EE SP1