这是对my previous question的后续问题。嵌套绑定表达式
#include <functional>
int foo(void) {return 2;}
class bar {
public:
int operator() (void) {return 3;};
int something(int a) {return a;};
};
template <class C> auto func(C&& c) -> decltype(c()) { return c(); }
template <class C> int doit(C&& c) { return c();}
template <class C> void func_wrapper(C&& c) { func(std::bind(doit<C>, std::forward<C>(c))); }
int main(int argc, char* argv[])
{
// call with a function pointer
func(foo);
func_wrapper(foo); // error
// call with a member function
bar b;
func(b);
func_wrapper(b);
// call with a bind expression
func(std::bind(&bar::something, b, 42));
func_wrapper(std::bind(&bar::something, b, 42)); // error
// call with a lambda expression
func([](void)->int {return 42;});
func_wrapper([](void)->int {return 42;});
return 0;
}
我越来越深的C++头文件编译错误:
functional:1137: error: invalid initialization of reference of type ‘int (&)()’ from expression of type ‘int (*)()’
functional:1137: error: conversion from ‘int’ to non-scalar type ‘std::_Bind<std::_Mem_fn<int (bar::*)(int)>(bar, int)>’ requested
func_wrapper(富)应该执行FUNC(DOIT(富) )。在真正的代码中,它封装了要执行的线程的函数。函数会由另一个线程执行的函数,doit坐在中间检查未处理的异常并清理。但在func_wrapper附加绑定食堂的事情了......
也许删除C++标记?这是直的C++ 0x – Anycorn 2010-04-29 20:33:33
保留这两个标签。 C++ 0x也是C++。 – jalf 2010-04-29 22:44:22
给PC-Lint一个旋风(来自www.gimpel.com)。它比Visual Studio提供更好更详细的错误消息非常好。但是,它非常昂贵。 – 2010-05-14 10:55:52