Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `(&)()` mean in a C++ compiler error message

Tags:

c++

g++

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?

like image 667
jds Avatar asked Dec 18 '25 04:12

jds


2 Answers

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.

like image 170
Nawaz Avatar answered Dec 20 '25 22:12

Nawaz


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

like image 21
Jarod42 Avatar answered Dec 20 '25 20:12

Jarod42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!