I have this code
struct A { A(); A(A&); };
struct B { B(const A&); };
void f(A);
void f(B);
int main() {
f(A());
}
To my surprise this fails with GCC and Clang. Clang says for example
Compilation finished with errors:
source.cpp:8:10: error: no matching constructor for initialization of 'A'
f(A());
^~~
source.cpp:1:21: note: candidate constructor not viable: expects an l-value for 1st argument
struct A { A(); A(A&); };
^
source.cpp:1:16: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
struct A { A(); A(A&); };
^
source.cpp:4:13: note: passing argument to parameter here
void f(A);
Why do they choose the first f
, when the second f
works fine? If I remove the first f
, then the call succeeds. What is more weird to me, if I use brace initialization, it also works fine
int main() {
f({A()});
}
They all call the second f
.
{...}
case. Does that explain why the{...}
case works? – Redbreast