传递指向成员函数的指针在MinGW-w64中进行编译,但在gcc中不进行编译 [英] Pass pointer to member function compiles in MinGW-w64 but not in gcc
问题描述
我有一个Worker
对象,该对象带有run()
个非静态成员函数.
I have a Worker
object with a run()
non static member function.
已创建一个对象:
Worker * worker = new Worker();
正在做
std::thread(Worker::run, worker);
在MinGW-w64(gcc 4.9.1)下但在来自Ubuntu的linux(gcc 5.2.1)下编译(一种工程),我得到了编译错误:
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
代码是使用-std=gnu++11
我知道在MinGW情况下,指向成员函数的指针具有带有Worker * this
参数类型的签名,从而可以像静态函数指针一样使用它.为什么在Linux 5.2.1 gcc中禁止这样做,我应该怎么写?
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?
编辑:我可以使用lambda或在Worker::run
之前添加&
来解决此问题,但是各种gcc版本为何接受还是不接受这一问题.这是MinGW还是gcc 4.9.1错误?
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?
推荐答案
您需要使用
std::thread(&Worker::run, worker);
live example
这篇关于传递指向成员函数的指针在MinGW-w64中进行编译,但在gcc中不进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!