2 回答
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).
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);