создание потоков C2672 и C2893 с кодом ошибки (c ++) - PullRequest
0 голосов
/ 26 февраля 2020

У меня проблемы с компиляцией этого кода, я получаю следующие сообщения:

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 компилятор

1 Ответ

2 голосов
/ 26 февраля 2020

Я не могу воспроизвести ошибку в VS2019 с C ++ 14. Однако я поместил ссылки в 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();
    }
}
...