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
9
votes
1 answer

C++ universal reference in constructor and return value optimization (rvo)

Why does rvalue optimization not occur in classes with constructor with universal reference arguments? http://coliru.stacked-crooked.com/a/672f10c129fe29a0 #include template struct C { template
tower120
  • 5,007
  • 6
  • 40
  • 88
9
votes
2 answers

Overload resolution with universal references

I have a function which can accept any type by universal reference, and would like to overload it for specific types (some of which are templated themselves, although I don't think that's important here). Unfortunately I can't quite seem to to get…
jleahy
  • 16,149
  • 6
  • 47
  • 66
8
votes
1 answer

C++ Overload resolution with universal reference function template which can't be changed

Suppose somewhere in my code is a function foo with a universal reference parameter, which I cannot change: template auto foo(T&& t) { std::cout<<"general version"<
davidhigh
  • 14,652
  • 2
  • 44
  • 75
8
votes
2 answers

How to store universal references

I need to store universal references inside a class (I am sure the referenced values will outlive the class). Is there a canonical way of doing so? Here is a minimal example of what I have come up with. It seems to work, but I'm not sure if I got it…
marton78
  • 3,899
  • 2
  • 27
  • 38
7
votes
2 answers

Passing a reference-to-function as a universal reference

I'm struggling to understand what exactly happens when passing a reference-to-function to a function as a universal reference (what type is being deduced). Let's suppose we have a function foo that takes a param as a universal…
rubix_addict
  • 1,811
  • 13
  • 27
7
votes
3 answers

Whether to use T const& or T&&

I'm curious if, in general, you are to use T&& (universal reference) instead of the classic T const& (l-value reference) for templated function parameters starting with C++11. What I'm especially curious is how you get around the fact that you are…
Austin Salgat
  • 416
  • 5
  • 13
7
votes
2 answers

Proper use of universal references

Before c++11, I used to write code like this: // Small functions void doThingsWithA(const A& a) { // do stuff } void doThingsWithB(const B& b) { // do stuff } void doThingsWithC(const C& c) { // do stuff } // Big function void…
7
votes
1 answer

std::forward of a function passed via universal reference?

Consider the two following: template void apply(Function&& function) { std::forward(function)(); } and template void apply(Function&& function) { function(); } In what case is there a difference,…
Vincent
  • 57,703
  • 61
  • 205
  • 388
7
votes
2 answers

Testing whether an object has been moved from

I'm working on testing whether or not my container wrappers are implementing URefs correctly. The only obvious way that I can think of is to try to figure out a way of detecting if an object has been moved. Is there a good way of testing to make…
OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
6
votes
1 answer

Cannot bind lvalue to A&&

I thought universal reference (T&&) is supposed to take any kind of reference. But the following doesn't work. I run into this problem when I try to be const-correct in a library that I am writing. I am new to C++ and haven't seen something like…
hamster on wheels
  • 2,771
  • 17
  • 50
6
votes
2 answers

Universal reference with templated class

Example: template class Bar { public: void foo(T&& arg) { std::forward(arg); } }; Bar bar; bar.foo(10); // works int a{ 10 }; bar.foo(a); // error C2664: cannot convert argument 1 from 'int' to 'int…
nikitablack
  • 4,359
  • 2
  • 35
  • 68
6
votes
1 answer

Why doesn't C++ have a const universal reference?

Scott Meyers says (for parameters to function templates): Universal references can only occur in the form "T&&"! Even the simple addition of a const qualifier is enough to disable the interpretation of "&&" as a universal reference. Why…
Sadeq
  • 7,795
  • 4
  • 34
  • 45
6
votes
0 answers

Are function template parameters T& and T&& ambiguous?

Consider these two overloads: template void foo(T &) {} template void foo(T &&) {} Are they potentially ambiguous? The following code compiles with Clang 3.4, but fails with GCC 4.8, which says the overloads (first one…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
6
votes
3 answers

rvalue or lvalue (const) reference parameter

I want to pass a parameter(s) (of some concrete type, say int) to the member function by r- or l- value (const) reference. My solution is: #include #include struct F { using desired_parameter_type = int; template<…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
5
votes
1 answer

What is the advantage of && in this code?

In the following code, what is the advantage of using &&? The code is from answer at Specialize same operator for different traits From this question, I get that an && argument means it is a reference that can be modified by the function. The…
rxu
  • 1,369
  • 1
  • 11
  • 29