The expression Qux (&)()
means a reference to a function taking no arguments and returning Qux
. This has already been explained by others. But that does not really address the specific detail of What does (&)()
mean in a [g++] C++ compiler error message?
When the compiler encounters a function call that it cannot resolve it tried to provide as much information in the error as possible. This particular type of error in g++ is printed as multiple lines, from the second on, the compiler will list the exact signatures of the candidates for overload resolution (i.e. known overloads of the function name found by lookup at the place of call). The first line is a bit trickier, as it represents the actual call:
this->bar(qux)
. If that was printed in the error message you would have to figure out what are the types of the different arguments, in particular what is the type of this
and the type of qux
.
The approach taken by the g++ implementation is to write something like a function signature that somehow represents the call. The problem is that it is actually impossible to represent the least restrictive signature that could be used, so it uses the same syntax with a slightly different meaning.
In particular, an lvalue-expression argument will be denoted by a (possibly const
) reference to the type of the expression, while an rvalue-expression is denoted by a non-reference type. Going back to the error message you got:
error: no matching function for call to `foo::bar(Qux (&)())`
This means that at the place of call, the first argument is an lvalue-expression denoting an object (well, functions are not objects but bear with me) of type Qux()
, that is a function taking no arguments and returning a Qux
by value.
In this particular case, the argument being a non-const lvalue-expression, the reading is not more confusing than parsing the type itself, but in other cases it is important to note that this is not really a signature. For example if the error message was:
error: no matching function for call to `foo::bar(Qux)`
It could be non-obvious why the only candidate is not a perfect match, does it not take a Qux
, which is what you are telling me that is being passed? Except that this syntax is representing a call with an rvalue-expression that cannot be bound by an lvalue-reference.
Similarly, if the argument was a const Qux
lvalue, the error would have been:
error: no matching function for call to `foo::bar(const Qux&)`
Indicating that the argument is an lvalue-expression but that the type is const Qux
which again cannot be bound by a non-const lvalue-reference.
Reading error messages is an art in itself, but it is an important part of the craft. You should pay attention whenever you have an error at the language that the compiler is using, as that will help you understand the issue the next time that you see a similar error message.