Quick Q: How to express constness of a forwarding reference?

Quick A: To express constness, a const reference is what is needed.

Recently on SO:

How to express constness of a forwarding reference?

How can I express that f does not modify its parameter?

If the function doesn't modify its parameter then there is no benefit to using a forwarding reference. You use a forwarding reference when you want to forward the parameter on to some other function which cares whether the argument is an lvalue or an rvalue (because maybe there are separate overloads for lvalues and rvalues, and the rvalue one can be more efficient).

If your function doesn't modify the argument then it shouldn't need to care whether it has an lvalue or rvalue, so it can just take const T& unconditionally. That can bind to both lvalues and rvalues, and promises not to modify the parameter.

template<typename T>
void f(const T& t) { ... }

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.