How the heck are you so knowledgable Peter? 0_0 |
The secret is that I spend too much time on sites like this, looking up stuff to be able to give correct answers. I'm not afraid of reading the standard if necessary. I also enjoy watching C++ talks (from CppCon, C++Now, Meeting C++, etc.) on YouTube, and listening to cppcast and cppchat. I like reading the trip reports after each standard meeting about all the new stuff that has been agreed for the next standard. I skim through the list of proposals (
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/) and read about the ones that I find interesting. In the process I pick up pieces of knowledge, but don't think I know everything by heart. There are simply too many edge cases and rule changes between versions to keep track of it all. I often have to compile or look things up in order to ensure that I am correct even when I think I know something.
vector<int> C = vector<int>(4);
This would really be instantiating two vectors right? When would the vector<int>(4) go out of scope? Only when memory of class is deallocated from heap and otherwise only at program termination?? |
The expression that you use to initialize the member would normally create a temporary object that would get destroyed after the member has been initialized. However, in this case, when the types are the same, the compiler has always been allowed to
elide the copy so that no temporary has to be created and no copying done. Compilers has been doing this kind of optimization for a long time but it was not until C++17 that it was made obligatory. In C++17 the vector wouldn't even need to have a copy or move constructor for this code to compile.