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
5
votes
5 answers

Having a function only accept non-const lvalues

I have a function which sorts two vectors with the first of them as ordering criterion. Its signature is template void sort(A&& X, B&& Y) { .. } The problem is that universal references would allow nonsense cases…
Dean
  • 6,610
  • 6
  • 40
  • 90
5
votes
1 answer

Type deduction of const references in templated functions

Below I have a template function named ProxyCall, which accepts an object, a member function and its arguments. It simply forwards the call to the member function. I would like to be able to call the function without using template qualifiers…
Alf
  • 752
  • 1
  • 7
  • 13
4
votes
0 answers

Why do signatures of make_shared and allocate_shared differ for the args to be forwarded?

In order to create a std::shared_ptr for a given type T, there are two ways among the others: std::allocate_shared and std::make_shared. The main difference (or at least, the one I'm interested in for this question) is that the former does all…
skypjack
  • 49,335
  • 19
  • 95
  • 187
4
votes
1 answer

How to combine type constraints & implicit conversions with C++11 universal references?

In a function I need to distinguish among lvalue and rvalue references, so the obvious path is to overload: void myfunc(A&& a); void myfunc(const A& a); This has exactly the desired behaviour, with well defined type and implicit conversions.…
Quartz
  • 141
  • 6
4
votes
1 answer

Member function template with universal reference won't accept lvalues

I've been trying to use a template member function to set a value inside of my class. I wanted to use a universal reference so that I could accept any variant of the correct type (e.g. T, T&, T&&, const T, const T&, const T&&) However, it seems that…
Nora Powers
  • 1,597
  • 13
  • 31
4
votes
1 answer

Variadic template constructor selection fails when argument is a reference

I have the following code: #include #include template struct A : T { template A(Args&&... params) : T(std::forward(params)...), x(0) { std::cout << "Member 'x' was default…
Svalorzen
  • 5,353
  • 3
  • 30
  • 54
4
votes
2 answers

C++ Unable to move unique_ptr with universal references

Consider this code: template T mov(T&& t){ return std::move(t); } int main(){ std::unique_ptr a = std::unique_ptr(new int()); std::unique_ptr b = mov(a); } The mov function should simply take a universal…
gexicide
  • 38,535
  • 21
  • 92
  • 152
3
votes
1 answer

How to correctly propagate the type of a universal reference?

I have a situation in my codebase where I must implement a generic form of std::get() which works for any kind of Tuple-like type. The function accepts a universal reference to a Tuple and returns a reference to the Ith element of the Tuple. I don't…
Jared Hoberock
  • 11,118
  • 3
  • 40
  • 76
3
votes
1 answer

Problems with conditional type-trait Universal Reference

So I have a function I use to check values and throw an exception if the value is not valid, otherwise pass the value back as it was received. I am trying to generalize this routine with universal reverences and type-traits. I feel I am close as my…
3
votes
1 answer

Equivalent static_asserts giving conflicting results for is_array<>

In the following snippet, an static assert passes and the other one fails: template constexpr bool is_array(Rng&& r) { // int*** debug = r; // uncomment this to debug r's type return std::is_array{}; // return…
3
votes
1 answer

Boost.python with universal reference / perfect forwarding?

I have been experimenting with C++11 universal references and perfect forwarding in some classes that also need to be accessed from Python using Boost.Python. I have code that works, but it requires some ugly template specialization outside of the…
jhand
  • 71
  • 5
3
votes
2 answers

A trick for optional reference using universal references?

I've found a trick to use optional reference in standard C++11. Do you think that this technique is reliable and the behaviour is well defined according to the the C++ standard ? // Optional reference using C++11 // V. Reverdy - 2013 #include…
Vincent
  • 57,703
  • 61
  • 205
  • 388
3
votes
3 answers

Can a defaulted template type be universal reference?

In the following, is && a universal reference ? template > void f(Function&& f = Function());
Vincent
  • 57,703
  • 61
  • 205
  • 388
2
votes
3 answers

universal reference c++11 code duplication

In my project I have function like this: bool VectorList::put(const Pair &p); This adds the Pair into the VectorList by copying the Pair. I can use it like this: Pair p { "key", "value" }; VectorList v; v.put(p); // or v.put(Pair{ "anotherkey",…
Nick
  • 9,962
  • 4
  • 42
  • 80
2
votes
2 answers

Lvalue/rvalue -nes encoding for universal references

I've been reading Effective Modern C++ and the following thing caught my attention: In Item 28 Scott writes: Together, these observations about universal references and lvalue/rvalue encoding mean that for this template template void…
Stvad
  • 360
  • 2
  • 17