Is there a difference between universal references and forwarding references?
Asked Answered
N

2

76

An argument to this function will bind to an rvalue reference:

void f(int && i);

However, an argument to this function will bind to either an rvalue or an lvalue reference:

template <typename T>  
void f(T && t);

I've often heard this referred to as a universal reference.
I've also heard it been called a forwarding reference.
Do they mean the same thing?
Is it only a forwarding reference if the function body calls std::forward?

Nester answered 17/9, 2016 at 22:24 Comment(2)
See isocpp.org/blog/2012/11/… for some background material.Filiate
@SamVarshavchik Did you actually read either the question or that blog?Brewmaster
B
86

Do they mean the same thing?

Universal reference was a term Scott Meyers coined to describe the concept of taking an rvalue reference to a cv-unqualified template parameter, which can then be deduced as either a value or an lvalue reference.

At the time the C++ standard didn't have a special term for this, which was an oversight in C++11 and makes it hard to teach. This oversight was remedied by N4164, which added the following definition to [temp.deduct]:

A forwarding reference is an rvalue reference to a cv-unqualified template parameter. If P is a forwarding reference and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction.

Hence, the two mean the same thing, and the current C++ standard term is forwarding reference. The paper itself articulates why "forwarding reference" is a better term than "universal reference."

Is it only a forwarding reference if the function body calls std::forward?

Nope, what you do with a forwarding reference is irrelevant to the name. The concept forwarding reference simply refers to how the type T is deduced in:

template <class T> void foo(T&& ); // <== 

It does not need to be subsequently forwarded .

Brewmaster answered 17/9, 2016 at 22:34 Comment(9)
Nice answer! Do you have any (non-trivial) example where you won't invoke a std::forward on a forwarding reference? You can also mention the auto&& case for local variables.Duiker
@Duiker template <class F> void foo(F&& arg) { use(arg); use_again(arg); consume(std::forward<F>(arg)); }Brewmaster
Self-explanatory, thanks, although you still end up with a forward :)Duiker
@Duiker std::shuffle, for instance.Waziristan
@Waziristan Thanks! Can you explain why it's not forwarded? So you don't "accidentally" end up moving the RNG? But then why passing by forwarding ref? Perhaps I should ask this as a new question...Duiker
@Duiker Basically, because it's used truly as a "universal reference" (to accept both lvalue and rvalue URNGs) and not for forwarding. See LWG1432.Waziristan
Reminder: a type is "cv-unqualified" if it is neither const nor volatile. see #15413537Quinquepartite
@Duiker Yes, a simple range-based for loop for example. I'm not really sure why they felt compelled to use the term forwarding reference instead of the well-established term universal reference that everyone was using anyway.Toenail
@Toenail My answer links to the paper which introduced forwarding reference, which provides an answer for "why they felt compelled to" introduce it. You can agree or disagree with the argument put forward there, but you don't have to be unsure of why.Brewmaster
J
19

Unfortunately, it's confusing, but they are nothing more than two names for the same thing.
Universal reference was proposed (I guess) by Meyers far ago (see here as an example).
Forwarding reference is picked up directly from the standardese. That's all.

Jessikajessup answered 17/9, 2016 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.