I apologize if the title of the question is inaccurate - but I am having difficulty understanding what is happening here.
Consider the following class:
struct foo { foo(foo&); };
The following are the errors:
void func(foo& f) { foo bar{f}; }
However, when I use auto:
void func(foo& f) { auto bar = foo{f}; }
I get (gcc):
test.cpp: In function 'void func(foo&)': test.cpp:6:21: error: no matching function for call to 'foo::foo(foo)' test.cpp:6:21: note: candidate is: test.cpp:2:5: note: foo::foo(foo&) test.cpp:2:5: note: no known conversion for argument 1 from 'foo' to 'foo&'
(clank)
test.cpp:6:10: error: no matching constructor for initialization of 'bar' auto bar = foo{f}; ^ ~~~~~~ test.cpp:2:5: note: candidate constructor not viable: expects an l-value for 1st argument foo(foo&); ^
Can someone explain why this is a mistake?
Thanks!
Edit: It works if I add a copy constructor to foo. However, I got the impression that declaring a variable + an explicit constructor call on the right side of the "=" syntax is handled specifically and is not a copy, but rather a direct initialization.
source share