Questions tagged [forwarding-reference]

A forwarding reference refers to a reference type that may either be an lvalue reference or an rvalue reference depending on its initializer. They allow the user to employ perfect-forwarding. The term applies either to a parameter of type T&& where T is a template parameter type or auto, as well as to auto&& local variables.

The term forwarding-reference is intended to replace deprecated name universal-reference.

The term applies to a parameter of type T&& where T is a template parameter type:

template <class T>
void f(T&& t) {}

or where T is auto:

[] (auto&& a) {}

as well as refers to auto&& local variables:

for (auto&& e : c) {}

The related wording proposal is http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4164.pdf

137 questions
186
votes
4 answers

What does auto&& tell us?

If you read code like auto&& var = foo(); where foo is any function returning by value of type T. Then var is an lvalue of type rvalue reference to T. But what does this imply for var? Does it mean, we are allowed to steal the resources of var? Are…
MWid
  • 3,809
  • 3
  • 17
  • 20
57
votes
2 answers

Is there a difference between universal references and forwarding references?

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 void f(T && t); I've often heard this referred…
37
votes
1 answer

What does `auto && e` do in range-based for-loops?

Assuming my current rule when programming with range-based loops says Use for(auto const &e :...) or for(auto &e:...) when possible over for(auto a: ...). I base this on my own experience and this question for example. But after reading about the…
towi
  • 20,210
  • 25
  • 94
  • 167
27
votes
1 answer

Why adding `const` makes the universal reference as rvalue

I have been reading about the universal references in Scott's last master piece about the c++11 and 14 with that being said despite an argument assigned to either lvalue or an rvalue type reference parameter there is something in between called…
RaGa__M
  • 2,341
  • 1
  • 17
  • 38
25
votes
3 answers

Syntax for universal references

This is an rvalue reference: void foo(int&& a); It does not bind to lvalues: int i = 42; foo(i); // error This is a universal reference: template void bar(T&& b); It binds to rvalues and it also binds to lvalues: bar(i); //…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
23
votes
1 answer

What's the standard/official name for universal references?

I know that if a variable or parameter is declared to have type T&& for some deduced type T, that variable or parameter is widely called a universal reference. The term universal reference was introduced by Scott Meyers in his original talk …
101010
  • 39,010
  • 10
  • 84
  • 149
22
votes
3 answers

Is this a forwarding reference?

The distinction between rvalue references and forwarding references was made clear enough in this example by Scott Meyers: Widget&& var1 = someWidget; // here, “&&” means rvalue reference (1) auto&& var2 = var1; // here, “&&” does…
Lorah Attkins
  • 3,879
  • 2
  • 22
  • 52
17
votes
3 answers

Passing literal as a const ref parameter

Imagine the following simplified code: #include void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Optimizations aside, what happens when 42 is passed to foo? Does the compiler stick 42 somewhere (on…
17
votes
1 answer

Why doesn't forwarding reference work in this case?

#include using namespace std; template void f1(T) {} template void f2(T&) {} template void f3(T&&) {} int…
xmllmx
  • 33,981
  • 13
  • 121
  • 269
17
votes
3 answers

What's the correct `enable_if` constraint on perfect forwarding setter?

Herb Sutter's Back to the Basics! Essentials of Modern C++ presentation at CppCon discussed different options for passing parameters and compared their performance vs. ease of writing/teaching. The 'advanced' option (providing the best performance…
bames53
  • 79,748
  • 13
  • 162
  • 229
17
votes
1 answer

Does an lvalue argument prefer an lvalue reference parameter over a universal reference?

While playing with universal references, I came across this instance where clang and gcc disagree on overload resolution. #include struct foo {}; template void bar(T&) { std::cout << "void bar(T&)\n"; } template
16
votes
2 answers

Can an identity alias template be a forwarding reference?

Consider the following snippet below: template using identity = T; template void foo(identity&&) {} int main() { int i{}; foo(i); } i is an lvalue, hence if foo declares a forwarding reference parameter, it should…
Marc Andreson
  • 3,225
  • 5
  • 31
  • 51
15
votes
1 answer

Do structured bindings and forwarding references mix well?

I know I can do auto&& bla = something(); and depending on the constness of the return value of something, I'd get a different type for bla. Does this also work in the structured bindings case, e.g. auto&& [bla, blabla] = something(); I would…
rubenvb
  • 69,525
  • 30
  • 173
  • 306
15
votes
3 answers

Perfect forwarding in a lambda?

With a function, one can write: template void f(T&& x) {myfunction(std::forward(x));} but with a lambda, we don't have T: auto f = [](auto&& x){myfunction(std::forward(x));} How to do perfect-forwarding in a lambda? Does…
Vincent
  • 50,257
  • 51
  • 171
  • 339
15
votes
1 answer

Why does std::forward return static_cast and not static_cast?

Let's have a function called Y that overloads: void Y(int& lvalue) { cout << "lvalue!" << endl; } void Y(int&& rvalue) { cout << "rvalue!" << endl; } Now, let's define a template function that acts like std::forward template void f(T&&…
gedamial
  • 1,466
  • 1
  • 14
  • 27
1
2 3
9 10