Cannot bind (function pointer) lvalue to (function pointer) rvalue when launching std::thread?

c++, c++11, multithreading, templates

Solution

You stumbled upon two bugs.

The first bug in GCC is that during the "perfect forwarding"-value category deduction, it thinks that `goo<int>` is an rvalue. But `goo<int>` is actually an lvalue. So instead of deducing `_Callable` to `void(void (*)(int), int)`, it should have deduced it to `void(&)(void (*)(int), int)`. Then reference collapsing would have yielded that lvalue reference as parameter type, instead of `void(&&)(void (*)(int), int)` like it incorrectly does with your GCC version.

During the actual initialization of the parameter, it also incorrectly rejects initialization of rvalue reference parameter by the function lvalue (I don't remember what the state of the working draft was when GCC4.5 was released - but possibly the draft had it ill-formed back then). For function type expressions, the Standard allows to initialize an rvalue reference to function type with an lvalue of function type.

Resolving a template id to a function lvalue is quite convoluted, so it doesn't surprise me GCC got it wrong (see http://llvm.org/bugs/show_bug.cgi?id=7505 and http://llvm.org/bugs/show_bug.cgi?id=7505 for two examples of how many rules interact for seemingly simple things).

Problem

I'm having an interesting issue with gcc 4.5.2. The following code ``` #include<thread> #include<iostream> using std::cout; void foo(int a){ cout<<a; } template <typename T> void goo(void (*fn)(T),T c){ fn(c); } int main(void) { std::thread TH; void (*ptr)(int)=foo; TH= std::thread(goo<int>,ptr,1); TH.join(); return 0; } ``` .. will not compile on gcc 4.5.2 with `error: cannot bind ‘void(void (*)(int), int)’ lvalue to ‘void (&&)(void (*)(int), int)’` followed by the second error `initializing argument 1 of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void(void (*)(int), int), _Args = {void (*&)(int), int}]’` However, this code does compile when the `template` is removed, and it also does compile with gcc 4.7.0 (with `template` in place). Even if this is a compiler issue, could somebody please explain what such an error means? I would be happy to find a way to do the binding (even if this is, say, automatic in gcc 4.7).

Original source