本文介绍了使线程C2672和C2893错误代码(c ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I have trouble compiling this code i get the following messages :

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

C2672 'std::invoke': no matching overloaded function found

C2893无法专用于功能模板'unknown-type std :: invoke(_Callable&&,_ Types& ...)noexcept()'

C2893 Failed to specialize function template '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编译器

Compiler : the default vs2019 compiler

推荐答案

我无法使用C ++ 14在VS2019中重现该错误.但是,我确实将引用放入了 std :: ref 包装器中,但是即使没有它们,我也不会遇到相同的错误(而是完全不同的错误).

I can't reproduce the error in VS2019 with C++14. I did however put the references in std::ref wrappers, but even without them I didn't get the same error (but a totally different).

我的猜测是,导致问题的原因还在于代码中的其他原因.

My guess is that it's something else in your code that is causing the problem.

#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();
    }
}

这篇关于使线程C2672和C2893错误代码(c ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:42