【发布时间】:2015-03-25 02:50:48
【问题描述】:
我正在阅读C++ concurrency in action。第 2.4 章介绍了一种 parallell_accumulate 算法。
我尝试 - 作为一个学习实验 - 用通用 lambda 替换那里使用的函子。
我将编译错误归结为:
#include <thread>
template <typename T>
struct f {
void operator() (T& result) { result = 1;}
};
int main() {
int x = 0;
auto g = [](auto& result) { result = 1; };
std::thread(f<int>(), std::ref(x)); // COMPILES
std::thread(g, std::ref(x)); // FAILS TO COMPILE
}
错误信息:
In file included from /usr/include/c++/4.9/thread:39:0,
from foo.cpp:1:
/usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<main()::<lambda(auto:1&)>(std::reference_wrapper<int>)>’:
/usr/include/c++/4.9/thread:140:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = main()::<lambda(auto:1&)>&; _Args = {std::reference_wrapper<int>}]’
foo.cpp:13:31: required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<main()::<lambda(auto:1&)>(std::reference_wrapper<int>)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.9/functional:1695:9: error: no type named ‘type’ in ‘class std::result_of<main()::<lambda(auto:1&)>(std::reference_wrapper<int>)>’
_M_invoke(_Index_tuple<_Indices...>)
^
我的编译器版本
$ g++ --version
g++ (Ubuntu 4.9.1-16ubuntu6) 4.9.1
为什么 lambda 编译失败,而仿函数编译失败?
编辑:如何使用通用 lambda 实现仿函数正在执行的操作(分配给 ref)?
【问题讨论】:
-
f并不完全等同于通用 lambda。有一个模板operator()()会更精确(它会产生同样的错误)。 -
std::bind指定在将reference_wrapper参数传递给目标函数之前对其进行解包,因此std::thread(std::bind(g, std::ref(x)))works as you intend。