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

C++ explicit universal reference constructor does not hide copy constructor?

Probably my understanding of explicit is insufficient, but I wonder why in the following code the copy constructor is not hidden by the unversal reference constructor when I declare the latter as explicit. struct A { A() = default; …
davidhigh
  • 14,652
  • 2
  • 44
  • 75
2
votes
1 answer

Turn casting / construction into a perfect forwardable function

SSCCE: #include using std::function; using std::forward; template ToType construct(FromTypes&&... fromTypes) { return ToType(forward(fromTypes)...); } class Maybe { public: …
1
vote
0 answers

Rvalue reference usage in sample is_copy_asignable implementation

I am watching Walter Brown's talk on CppCon 2014 'Modern Template Metaprogramming: A Compendium'. In part two, he demonstrates the usage of decltype and declval with a sample implementation of is_copy_assignable. Here is a link to the video, with…
1
vote
1 answer

How to boost::bind a template member function which takes a universal reference as a parameter

I've been trying to use boost::bind to post a call to a member function onto an io_strand but have been getting errors. I have manged to create a simplistic equivalent example of what I am trying to do and have seen the same error in the following…
zephyrJ
  • 58
  • 6
1
vote
2 answers

Moving elements from std::vector to std::vector>

What is the most correct and efficient way to std::move elements from a vector of a certain type (T1) into a vector of an std::pair of that same type (T1) and another type (T2)? In other words, how should I write MoveItems()? #include //…
1
vote
0 answers

const applied to "universal reference" parameter

I've stumbled upon Scott Mayers article on universal references, link. From what I understood universal reference, that is some type T&& can mean an rvalue or lvalue type in different contexts. For example: template void f(T&& param); …
codekiddy
  • 5,897
  • 9
  • 50
  • 80
1
vote
1 answer

VS 2013 fails to specialize function template with universal reference and return type depending on template parameter

VS 2013 says that it cannot specialize the function template in the following code: struct W { }; template typename T::result_type f (const W & w, T && t) { return 0; } /* ... */ struct V { typedef int result_type; }; W w {}; V v…
JohnB
  • 13,315
  • 4
  • 38
  • 65
1
vote
1 answer

limit universal reference to a single type using aliasing

I have a member function of a class in which I'd like to use perfect forwarding for one of the parameters. However, the function being forwarded to only accepts a single argument of type c2Type, so I'd like the calling function also to only accept…
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
1
vote
2 answers

Template deduction/overload resolution favors T&& over const T&

I have a pair of function templates defined like so: template Foo f(const CollectionType& v) { return Foo(v); // copies v into a member variable } template
dlf
  • 9,045
  • 4
  • 32
  • 58
1
vote
1 answer

Universal reference producing different instances with the same address

I was experimenting with Universal references inspired by Scott Meyers article on the subject. So I tried the following: #include #include template T& f(T&& var){ std::cout<< &var << std::endl; return…
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
1
vote
2 answers

Universal reference in constructor causes failure, can't assign callable functor to std::function

In the following complete test case, if I use the first ctor taking a functor by value and moving it into place then the code compiles and works as expected. However if I use the universal reference ctor it fails to compile (I've included the clang…
111111
  • 15,686
  • 6
  • 47
  • 62
1
vote
1 answer

Why do "normal" implementations of copy/move functions in derived classes behave differently depending on how they're defined?

I'm confused about the behavior I'm seeing when derived class copy and move functions call their base class versions. I have a base class with various constructors that tell me when they're called: #include class Base { public: …
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
1
vote
1 answer

Why do const lvalues bind differently from const rvalues given T&& and const T& overloads?

For this code (available at http://ideone.com/Mo7fQr) template void f(const T&) { std::cout << "const T& overload\n"; } template void f(T&&) { std::cout << "T&& overload\n"; } int main() { const int x = 0; f(x); …
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
1
vote
1 answer

universal reference ignores top level cv qualifier

Can anyone tell me why univeral references loose the top-level cv qualifies? I expected the output would return true for const on the second and third function calls in the following code. #include #include using namespace…
Blair Davidson
  • 901
  • 12
  • 35
0
votes
1 answer

universal reference for template type error

Excuse the bad title... So, normally in a function like this: template void f(T&& i){ } T&& is a universal reference. In a context like this it is an rvalue-reference: template struct s{ void f(T&& t){} }; This makes…
lo tolmencre
  • 3,804
  • 3
  • 30
  • 60