1

As far as I'm aware C++ standard does't distinguish between rvalue references and forwarding references.

I'd like to know the reason behind that.

If I want to write a template function that only accepts lvalue expressions I do the following:

template<typename T>
void func(T& param); 

But if I want to write a template function that only accepts rvalue expressions:

template<typename T>
void func(T&& param);

it turns out that this function also accepts lvalue expressions.

So the question is: what is the logic behind this language design?

beginpluses
  • 497
  • 2
  • 9
  • Maybe a duplicate of [Concise explanation of reference collapsing rules](https://stackoverflow.com/questions/13725747/concise-explanation-of-reference-collapsing-rules-requested-1-a-a-2). – François Andrieux Jun 16 '19 at 13:23

1 Answers1

0

It's also referred to as a universal reference.

As the name implies, it's a universal reference, allowing a lot of different types.

If you want it to stay an r-value ref (T&&), you use, std:forward<T>(param), otherwise you make sure it's the type you want it to be when you pass it to wherever you use it.

JohnkaS
  • 622
  • 8
  • 18