Reputation: 1138
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).
Upvotes: 3
Views: 2112
Reputation: 507005
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).
Upvotes: 6
Reputation: 137820
An lvalue is an expression representing an object that can have its address taken. For example, the left-hand side of an assignment expression to a primitive type must be an lvalue: int i; i = 3;
is OK whereas 5 = 3
is not. The difference is that &i
is OK but &5
is not.
goo<int>
is an lvalue of function type, but expressions of function type are essentially useless in C and C++. They are invariably converted to function pointers, by taking the address. The resulting pointer is not an lvalue, since that would be taking the address of the address.
The bug in G++ is in when the address is implicitly taken. Apparently the conversion occurs before template deduction when you pass a non-template goo
but after when you pass goo<int>
. You need it to happen earlier, so the constructor doesn't attempt to receive a reference.
The unary &
operator suffices to force the conversion:
TH= std::thread(&goo<int>,ptr,1);
Upvotes: 4