2 ms·
In C++ return values have exactly the same semantics as your 'out' parameters, so I'm not sure what they would add. Also pass-by-reference vs in-out has very v
by gpderetta 5y ago
In C++ return values have exactly the same semantics as your 'out' parameters, so I'm not sure what they would add.
Also pass-by-reference vs in-out has very visible semantic differences on the face of aliasing, exceptions and abnormal termination (when referring to shared memory).
- adrian_b 5y agoAfter 2011, they kind-of have it, but not previously. For non-const parameters the compiler normally assumes that they are in-out. Whether a parameter is out or in-out is very important for types with destructors. Before calling a function, an "out" parameter is raw memory, on which a destructor should never be called. After the function returns, the "out" parameter contains a new object, possibly with allocated resources. Before calling a function, an "in-out" parameter contains an existing object. If inside the function a new object is assigned to the parameter, the destructor must be invoked first, usually implicitly, inside an "operator=". Before C++ 2011, most C++ compilers treated the return value as an in-out parameter, like every other non-const parameter, so they allocated a temporary and constructed a default object in the temporary, before calling the function. One of the major changes in C++ 2011 was a collection of extra syntax features and library tricks to achieve the so-called move semantics, i.e. to avoid unnecessary constructions and destructions of temporaries, all of which had their origin in the lack of distinction between out and in-out parameters. Aliasing of "out" or "in-out" function parameters is something that does not have defined behavior in any programming language. When 2 aliased in-out parameters are passed by reference, the value after the function returns is unpredictable, because it depends on the order of interleaving of the changes to the 2 parameters inside the function. When 2 aliased in-out parameters are passed by value, the value after the function returns is unpredictable, because the function receives 2 copies, whose values are correct immediately before return, but the final value seen after return depends on the order in which the 2 values are copied over the original aliased location, which is undefined. Similar effects exist for exceptions and abnormal termination, the details may differ between pass-by-value and pass-by-reference, but the result is always undefined, so it does not matter. While aliasing of "out" and "in-out" parameters cannot have defined behavior in any language, regardless of the method used for parameter passing, aliasing of "in" parameters is always OK, and here again it does not matter whether they are passed by value (when multiple copies are passed) or by reference (when multiple references to a single immutable object are passed), the behavior is indistinguishable in this case.
- 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.