May the compiler silently convert from const to const& and vice versa?

Viewed 108

The thought behind this is that the value won't be changed so that we don't need to care about the memory adress.

Of course there are special cases like (maybe only) polymorphism where this is not possible.

1 Answers

A value copy of an object might not be taken if const& is substituted for const.

If that has a side effect then the compiler is not allowed to make the substitution.

In this respect, the rule differs from return value optimisation where a value copy is allowed to be dropped even if there are side effects.

Related