3 ms·
> Since objects are values in C++, you need to know the size of things much more often. It's not like C++ prevents reference semantics. If you need the flexibi
by cbsmith 10y ago
> Since objects are values in C++, you need to know the size of things much more often.
It's not like C++ prevents reference semantics. If you need the flexibility/abstraction, that's what you do. That said, value semantics actually provide clearer abstractions in interfaces (for example, no question about aliasing or whether other code is referencing the value), and generics can do a lot to abstract the extra size exposure from value semantics.
- humanrebar 10y ago> no question about aliasing or whether other code is referencing the value Well, actually, there's quite a bit of convention here. The 'could cause undefined behavior' code I hashed out as an example uses a const reference, which is the widely accepted best practices for copy semantics. There's sort of an implied 'and the concrete implementation will copy this value if it needs it for longer than the duration of the method call', but that's a convention; it's not enforced by compiler. We could pass these sorts of parameters by value, but there are reasons people like to avoid passing by value as a default. Engineers could define interfaces with only pass-by-value arguments, but there
- cbsmith 10y agoPeople do use the reference to const as a way to imply something similar to value semantics... but it's a huge mistake to say that implies that a copy of the value is implied. Ever since rvalue references were introduced, the rationale for having a const ref interface to avoid an unnecessary copy kind of became an anachronism.