问题
I have a Worker
object with a run()
non static member function.
An object has been created:
Worker * worker = new Worker();
Doing:
std::thread(Worker::run, worker);
Compiles (an works) under MinGW-w64 (gcc 4.9.1) but under linux (gcc 5.2.1) from Ubuntu, I get the compilation error:
Invalid use of non-static member function
The code is compiled with -std=gnu++11
I understand that in the MinGW case, the pointer to the member function has a signature with a kind of Worker * this
parameter, allowing to use it like a static function pointer. Why is this forbidden in the linux 5.2.1 gcc, and how should I write that?
EDIT : I can solve this using a lambda or by adding &
before Worker::run
, but the question why it is accepted or not by various gcc versions remains. Is this a MinGW or gcc 4.9.1 bug?
回答1:
You need to use
std::thread(&Worker::run, worker);
live example
来源:https://stackoverflow.com/questions/33387462/pass-pointer-to-member-function-compiles-in-mingw-w64-but-not-in-gcc