Question

What does this error mean?

error: no matching function for call to `foo::bar(Qux (&)())`

I understand that the compiler (g++) is failing to match a function call with a foo member function. I am asking specifically about the extra stuff at the end; this bit: (&)().

For reference, my function call is like this:

// inside another member function of `foo`
Qux qux();
this->bar(qux);

And the signature for bar is:

virtual void bar(Qux&);

The compiler is also saying that the only candidate is:

virtual void bar(Qux&);

How does my function call signature differ from my definition signature, and what does (&)() mean?

Était-ce utile?

La solution

Well, (&)() alone doesn't mean anything. (&)() is just a part of Qux (&)() which means reference to a function which takes nothing and return Qux. And this arises because of this:

Qux qux();      //PROBLEM!
this->bar(qux);

The first line here does NOT declare an object. It declares a function instead.

Search for vexing parse in C++, on this site, you'll see lots of topic on it, that discuss this problem in detail.

Autres conseils

You should use Qux qux; (or alternatively in C++11 Qux qux{};) to avoid the most vexing parse.

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.

Qux qux();

declares a function taking no arguments and returning Qux. You should write

Qux qux;

to define an object of type Qux.

Qux qux();

That is a function declaration.

this->bar(qux);

That is attempting to pass a reference to the function qux to bar.

If you wanted to default-construct qux, leave out the ():

Qux qux;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top