Questions tagged [universal-reference]

A universal 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.

Both auto&& and the T&& in template<class T> void f(T&&); are universal references.

Scott Meyers did an excellent talk on the topic, which can be found here.

When adding this to the draft of the next C++ standard, the focus was on the perfect-forwarding, and so the term forwarding reference was chosen.

63 questions
71
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…
65
votes
1 answer

Should all/most setter functions in C++11 be written as function templates accepting universal references?

Consider a class X with N member variables, each of some copiable and movable type, and N corresponding setter functions. In C++98, the definition of X would likely look something like this: class X { public: void set_a(A const& a) { _a = a; } …
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
50
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
  • 21,587
  • 28
  • 106
  • 187
27
votes
1 answer

Why does a range-based for statement take the range by auto&&?

A range-based for statement is defined in §6.5.4 to be equivalent to: { auto && __range = range-init; for ( auto __begin = begin-expr, __end = end-expr; __begin != __end; ++__begin ) { for-range-declaration =…
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
25
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
  • 41,839
  • 11
  • 94
  • 168
18
votes
3 answers

Behavior of C++ template function

Let's say I have this function: bool f(int&& one, int&& two) { } If I attempt to call it with this code: int x = 4; f(x, 5); the compiler will complain that it cannot convert x from lvalue reference to rvalue reference, which is correct. Now if…
superkinhluan
  • 733
  • 7
  • 23
18
votes
4 answers

When not to use `auto&&`?

auto&& mytup = std::make_tuple(9,1,"hello"); std::get<0>(mytup) = 42; cout << std::get<0>(mytup) << endl; Is there a copy/move involved (without RVO) when returning from make_tuple? Is it causing undefined behavior? I can both read write…
balki
  • 26,394
  • 30
  • 105
  • 151
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
15
votes
6 answers

Get advantages of an universal reference, without an universal reference

Problem Let's assume a function func that takes any container in the form Container (that is a container that takes as first template argument a type and as second an std::size_t defining how many arguments there are in the…
Shoe
  • 74,840
  • 36
  • 166
  • 272
15
votes
1 answer

universal reference vs const reference priority?

When I consider the two following overloads: template void f(const T&... x); template void f(const T& x); I have the guarantee that f(x) will always call the second function and will never lead to an ambiguity. In a sense the…
Vincent
  • 57,703
  • 61
  • 205
  • 388
14
votes
3 answers

How will concepts lite interact with universal references?

I looked recently at this video explaining the ideas of concepts lite in C++, which are likely to appear this year as a TS. Now, I also learned about universal references/forwarding references (as described here) and that T&& can have two meanings…
13
votes
1 answer

Why does this function return an lvalue reference given rvalue arguments?

The following definition of a min function template constexpr auto min(T&& t, U&& u) -> decltype(t < u ? t : u) { return t < u ? t : u; } has a problem: it seems that it's perfectly legal to write min(10, 20) = 0; This…
13
votes
2 answers

Is this a universal reference? Does std::forward make sense here?

Consider this snippet of code, which uses the common idiom of having a function template construct an instance of a class template specialized on a deduced type, as seen with std::make_unique and std::make_tuple, for example: template
Oktalist
  • 14,336
  • 3
  • 43
  • 63
10
votes
4 answers

What kind of problems for not forwarding universal reference?

As far as I know, in C++11, universal reference should always be used with std::forward, but I am not sure of what kind of problem can occur if std::forward is not used. template void f(T&& x); { // What if x is used without…
Vincent
  • 57,703
  • 61
  • 205
  • 388
10
votes
2 answers

Specializating a function template that takes a universal reference parameter

How do I specialize a function template that takes a universal reference parameter? foo.hpp: template void foo(T && t) // universal reference parameter foo.cpp template<> void foo(Class && class) { // do something…
David Stone
  • 26,872
  • 14
  • 68
  • 84
1
2 3 4 5