【发布时间】:2017-11-05 14:45:09
【问题描述】:
我有一个void fun(vector<int> & v),我想在实例化线程thread t(fun, v); 时将一个向量传递给它。在C++14 clang 4 编译失败,在MSVC it runs passing a copy 中起作用。
#include <thread>
#include <vector>
#include <iostream>
using namespace std;
void fun(vector<int> & v) {
v.push_back(13);
}
int main(){
vector<int> v;
thread t(fun, v);
t.join();
cout << v.size();
}
gcc 5.4.0 错误示例:
在 /usr/include/c++/5/thread:39:0 包含的文件中, 来自 source_file.cpp:1: /usr/include/c++/5/functional: 在“struct”的实例化中 std::_Bind_simple))(std::vector&)>': /usr/include/c++/5/thread:137:59:需要来自 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(std::vector&); _Args = {std::vector
&}]' source_file.cpp:12:21: 从这里需要 /usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in '类 std::result_of))(std::vector&)>' typedef typename result_of<_callable>::type result_type; ^ /usr/include/c++/5/functional:1526:9: 错误:没有名为“type”的类型 '类 std::result_of))(std::vector&)>' _M_invoke(_Index_tuple<_indices...>)
所以 1) 什么是 c++ 标准在这个问题上的立场; 2) 有没有办法解决它(不是passing a pointer,也不是+1 额外lambda expression as wrapper)?
【问题讨论】:
标签: c++ multithreading binding arguments c++14