In talk about initialization lists , I realized that Stroustrup basically says that the new construction syntax with curly braces is supposed to be a common replacement for all previous build syntaxes
X x1();
Instead, the new syntax is supposed to be used evenly as a possible replacement that you can use in every situation ... again, what is the main message that I took from his conversation. Maybe I misunderstood him.
So the question is, how is the general syntax? Is it possible to never use the old construction in the new C ++ 11 code, or are there situations when you need to return?
This question was initiated / motivated when I came across the following error, which, in my opinion, is a compiler error (but I would be glad if you could fix it).
struct X {}; int main() { X x; X& y{x};
which does not compile on g ++ 4.7.1 and does not work on ideone 4.5.1 .
prog.cpp: In function 'int main()': prog.cpp:5:9: error: invalid initialization of non-const reference of type 'X&' from an rvalue of type '<brace-enclosed initializer list>' prog.cpp:6:9: error: invalid initialization of non-const reference of type 'X&' from an rvalue of type '<brace-enclosed initializer list>'
Note that it works when I replace X with int .
source share