Ank*_*rya 6 c++ multithreading auto c++14
我遇到了问题,std::thread因为它不接受采用自动指定参数的函数.以下是一些示例代码:
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
void seev(const auto &v) // works fine with const vector<int> &v
{
for (auto x : v)
cout << x << ' ';
cout << "\n\n";
}
int main()
{
vector<int> v1 { 1, 2, 3, 4, 5 };
thread t(seev, v1);
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是编译器说:
[Error] no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, std::vector<int>&)'
Run Code Online (Sandbox Code Playgroud)
为什么会这样?这是语言或GCC(4.9.2)的问题吗?
Dew*_*wfy 13
想想auto作为模板参数,那么你的函数看起来像:
template <class T>
void seev (const T &v) ...
Run Code Online (Sandbox Code Playgroud)
如果没有明确的类型规范,C++就无法体现模板.这就是你得到错误的原因.要解决问题(使用模板参数声明),您可以使用:
thread t (seev<decltype(v1)>, std::ref(v1));
Run Code Online (Sandbox Code Playgroud)
Bau*_*gen 13
void seev (const auto &v)
Run Code Online (Sandbox Code Playgroud)
是无效的C++(然而,它是为C++ 17提出的).gcc会告诉你有没有编译过-pedantic.
| 归档时间: |
|
| 查看次数: |
984 次 |
| 最近记录: |