【问题标题】:making threads C2672 and C2893 error code ( c++ )制作线程 C2672 和 C2893 错误代码 (c++)
【发布时间】:2020-06-10 12:26:56
【问题描述】:

我在编译此代码时遇到问题,我收到以下消息:

C2672 'std::invoke': 找不到匹配的重载函数

C2893 无法专门化函数模板 'unknown-type 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 没有invoke,你的代码也没有调用它。那么……是谁?您能否提供minimal reproducible example 以及 yoru 编译器、标准库和您的编译选项?
  • 为什么都是指针?
  • @NicolBolas 我使用 vs2019 (默认 c++ 编译器)并且 std::invoke 在胎面标题内被调用( c++14 #include )
  • @TedLyngmo 使用了指针,以便我拥有一个动态的 std::thread 数组,并能够使用新的 std::thread() 使它们运行;
  • 为什么不使用std::vector&lt;std::thread&gt; 或std::list&lt;std::thread&gt; 呢?

标签: c++ c++14


【解决方案1】:

我无法使用 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();
    }
}

【讨论】:

  • std::ref 工作,非常感谢我真的很感激
猜你喜欢
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2013-07-17
相关资源
最近更新 更多