Pass pointer to member function compiles in MinGW-w64 but not in gcc

帅比萌擦擦* 提交于 2019-12-24 00:05:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!