Among the many benefits of const qualification is to make an API more understandable, example:
template<typename T> int function1(T const& in);
// clearly, the input won’t change through function1
With the introduction of rvalue references, one can benefit from perfect forwarding but often const qualifiers are removed, example:
template<typename T> int function2(T&& in);
// can explicitly forward the input if it's an rvalue
Apart from documentation, is there a good way to describe that function2 won’t change its input?
You could say this:
where you have something like:
This way, the template will only be usable if the universal reference is either a const-reference (so
T = U const &
) or an rvalue-reference (soT
is not a reference).That said, if the argument is not going to be changed, you could just use
T const &
and be done with it, since there's nothing to be gained from binding mutably to temporary values.Yes. Stick with the C++03 solution:
The benefits of perfect forwarding are that you don't want to assume if something is
const
or non-const
, lvalue or rvalue. If you want to enforce that something is not modified (i.e. that it isconst
), then explicitly say so by addingconst
.You could do this:
However everyone who read your code would wonder why you've used rvalue references. And
function1
would cease to accept lvalues. Just useconst &
instead and everyone will understand. It is a simple and well understood idiom.You don't want to perfectly forward. You want to enforce immutability.