2

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?

galinette
  • 8,896
  • 2
  • 36
  • 87

1 Answers1

3

You need to use

std::thread(&Worker::run, worker);

live example

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • This works, thanks. Any idea why gcc 4.9.1 accepts it without ampersand? Is this related to anything which changed in the standard? – galinette Oct 28 '15 at 09:41
  • @galinette It shouldn't work, and it also does not work on GCC 4.9.1 [here](http://melpon.org/wandbox/permlink/J3LAPxodlucIiEyE). – m.s. Oct 28 '15 at 09:43
  • MinGW-w64 bug then? That's strange since it's a gcc port which should not affect the parser. Mistery then... – galinette Oct 28 '15 at 10:00
  • @galinette sure, that's possible – m.s. Oct 28 '15 at 10:01