3 ms·
> 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
by 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.