问题
I have trouble compiling this code i get the following messages :
C2672 'std::invoke': no matching overloaded function found
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 ) ;
and this gets the same error as well :
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 ) ) ;
Compiler : the default vs2019 compiler
回答1:
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();
}
}
来源:https://stackoverflow.com/questions/60417178/making-threads-c2672-and-c2893-error-code-c