Step 3: Your temporary initializer_list is moved into the variable representing the parameter that the constructor takes by value, which [shouldn't] mean all of it's elements are also moved.
Step 5: 4 objects are constructed. 4 destructors are called. Most of your constructors are not logically correct or the message in the destructors would probably be more informative (although I would expect logging this would be more useful than n.)
Okay I've touched a critical side of the language.
I can't blow my mind up just because a compiler has bugs. Stroustrup doesn't tell anyone which compiler he uses but I NEED to know which one is the most standardized.
I can't learn/develop in C++ if I have bugged compilers all over the place.
I use VC++ on Windows, and I have never had any problems.
I can't learn/develop in C++ if I have bugged compilers all over the place.
Compilers are programs too, they have bugs. If you spot one, report it to help others.
There aren't compiler bugs in this example though. There is a language specification bug (single-element initializer list calling the std::initializer_list constructor) and a quality of implementation issue (gcc moving your objects more times than necessary).
I think VC++ on Windows and Clang everywhere else is the perfect pair.
What do you think?
I think the choice of the compiler depends on what you need it for.
At a previous job, my programs ran on p795s, T5s and Itaniums, so I used IBM XL C++, Oracle Studio, and HP aCC (with a pass in Clang for warnings/static analysis). Today, I mostly run on intel servers, so the main compilers in use are intel, gcc, and msvc, with, again, clang for syntax checking.
When an actual initialiser list is present, the GNU compiler (current version) without the dubious -fno-elide-constructors behaves sensibly.
1 2 3 4 5 6
int main()
{
Foo a("Hello");
Foo b{ a, a };
}
-------- clang++/libc++ ------------
ctor (Hello)
copy ctor (copying Hello)
copy ctor (copying Hello)
initializer list
Destructor of
Destructor of
Destructor of
Destructor of Hello
--------- g++/libstdc++ ------------
ctor (Hello)
copy ctor (copying Hello)
copy ctor (copying Hello)
initializer list
Destructor of
Destructor of
Destructor of
Destructor of Hello
I need it for learning the most Standard C++ as possible.
clang with libc++ is probably the best bet, then. (on coliru, use -stdlib=libc++). Clang's lead developer is also the current C++ standard's project editor, and libc++'s lead developer invented move semantics, unique_ptr, and other important parts of C++11