c++ - 制作线程 C2672 和 C2893 错误代码 (c++)
问题描述
我在编译此代码时遇到问题,我收到以下消息:
C2672 'std::invoke':找不到匹配的重载函数
C2893 无法专门化函数模板“未知类型 std::invoke(_Callable &&,_Types &&...) noexcept()”
static auto f = [ ] ( int offset , int step , std::vector<Vert>& vertices , const Transform &transform ) {
// do stuff
};
// create threads
int max_threads = 4 ;
std::thread **active_threads = new std::thread * [ max_threads + 1 ] ;
for ( int i = 0 ; i < max_threads ; i++ )
active_threads [ i ] = new std::thread ( f , i , max_threads , vertices , transform ) ;
这也得到了同样的错误:
int max_threads = 4 ;
static auto f = [ ] ( Vert *verts , int offset , int step , const std::vector<Vert> &vertices , const Transform& transform ) {
// do stuff
}
// create threads
std::vector<std::thread> active_threads ;
for ( int i = 0 ; i < max_threads ; i++ )
active_threads.push_back ( std::thread ( f , verts , i , max_threads , vertices , transform ) ) ;
编译器:默认的 vs2019 编译器
解决方案
我无法使用 C++14 重现 VS2019 中的错误。然而,我确实将引用放在了std::ref
包装器中,但即使没有它们,我也没有得到相同的错误(但完全不同)。
我的猜测是导致问题的代码中的其他内容。
#include <iostream>
#include <list>
#include <thread>
#include <vector>
struct Vert {};
struct Transform {};
static auto f = [](int offset, int step, std::vector<Vert>& vertices,
const Transform& transform) {
std::cout << offset << ' ' << step << ' ' << vertices.size() << '\n';
};
int main() {
std::list<std::thread> active_threads;
int max_threads = 4;
std::vector<Vert> vertices;
Transform transform;
for(int i = 0; i < max_threads; i++)
active_threads.emplace_back(f, i, max_threads, std::ref(vertices),
std::ref(transform));
for(auto& th : active_threads) {
th.join();
}
}
推荐阅读
- r - groupby 在 R 中的类字符列上进行汇总
- azure - Azure Disk Encryption (ADE) for AKS cluster node's OS & Data disks
- c++ - Invalid application of ‘sizeof’ to incomplete type ‘RSA’ {aka ‘rsa_st’}
- javascript - 如何覆盖 MUI 芯片中的标签样式
- python - 在类似于 pandas 的字符串上使用 Dask 中的 to_datetime 方法
- python - Pandas groupby 并从每个正负类型中选择前 5 个和后 5 个值
- minecraft - 被击中后的暴徒无敌
- mockito - Apache POI 行迭代问题
- heroku - Heroku 上的部署失败,没有真正的错误消息
- python - Python - 是否可以使用不同的变量来引用多个不同的变量?