2 ms·
[regarding return values, I was mostly referring to RVO that bypasses moves, but close enough]. So you are saying that assignment through out parameters will a
by gpderetta 5y ago
[regarding return values, I was mostly referring to RVO that bypasses moves, but close enough].
So you are saying that assignment through out parameters will act as placement new, by constructing a new object? That's interesting, although it would be a big change from the existing language semantics (assignment is very different from initialization at the moment) so it would be very hard to retrofit.
Does that means one wouldn't be able to pass an existing object as an out parameter and would need to pass raw memory? Or the caller is responsible of destroying the object first? That has implications for exception safety.
- adrian_b 5y agoAll this is done differently in C++, due to historical reasons determined by decisions taken between 1980 and 1984, even before the name C++ was adopted (in January 1984). If objects with constructors and destructors had been introduced in a language with distinct "in", "out" and "in-out" parameters, everything could have done in a much simpler way than in C++ and without needing to introduce a lot of weird syntax and non-intuitive mechanisms. In a language with distinct "out" parameters, yes the compiler ensures that the "out" parameters are raw memory, i.e. registers or free stack locations, or if the space previously occupied by temporaries is reused, the temporaries are destroyed before invoking the function. Inside the function, the compiler ensures that any "out" parameters must be initialized somehow, e.g. by a "new", before any kind of access. In a language with "out" parameters, assignment would be defined as having an "out" left parameter, so there would exist no copy constructor or move constructor and "operator =" would be what in C++ is named now as copy constructor. The compiler would take care to destroy any left-hand operands of assignment before the assignment, when necessary. The same for out parameters, if they have been declared but not initialized, they are raw memory so they are passed as they are. If they have already been initialized, the compiler ensures that they are implicitly destroyed. All these rules are obvious enough, and they are much simpler than the very complex rules that you must take into account to understand what a C++ compiler does with temporaries and what happens differently when you add either "&" or "&&" to the type, and so on. Some people believe that having to write 4 member functions, copy constructor, move constructor, copy assignment and move assignment, as it is mandatory in C++, is an opportunity to sometimes make a performance optimization, but I do not agree. I believe that the case when a worthwhile optimization can be made in a copy assignment function (e.g. by reusing some allocated memory, which slightly bypasses the same reusing that would have been done in the memory allocator otherwise) are far to rare to justify the waste of time with extra function definitions for every class. Initially C++ did not have a copy constructor, but only an "operator=", which mistakenly assumed a pre-existing object as its left-hand side. Initializations by copy were done by the default constructor followed by "operator=". This was inefficient, so the copy constructor was added, but the right solution would have been to redefine the semantics of "operator=", to match that what is now called copy constructor. Then initializations would have been done with "operator=" and for assignments the compiler would have invoked implicitly the destructor. The programmer would have always written only 1 "operator=" function (and a simpler function, corresponding to the current copy constructor) and there never would have been any need for the other 3 functions. This would have almost always resulted in the same performance as now, but with a far less burden on the programmer.