Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:57:12+00:00 2026-06-17T09:57:12+00:00

This is an rvalue reference: void foo(int&& a); It does not bind to lvalues:

  • 0

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<typename T>
void bar(T&& b);

It binds to rvalues and it also binds to lvalues:

bar(i);   // okay

This is an rvalue reference:

template<typename T>
struct X
{
    void baz(T&& c);
};

It does not bind to lvalues:

X<int> x;
x.baz(i);   // error

Why do universal references use the same syntax as rvalue references? Isn’t that an unnecessary source of confusion? Did the committee ever consider alternative syntaxes like T&&&, T&*, T@ or T&42 (just kidding on that last one)? If so, what were the reasons for rejecting alternative syntaxes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:57:13+00:00Added an answer on June 17, 2026 at 9:57 am

    A universal reference such as T&& can deduce T to be an “object type“, or a “reference type“

    In your example it can deduce T as int when passed an rvalue, so the function parameter is int&&, or it can deduce T as int& when passed an lvalue, in which case the function parameter is int& (because the reference collapsing rules say std::add_rvalue_reference<int&>::type is just int&)

    If T isn’t deduced by the function call (as in your X::baz example) then it can’t be deduced to int&, so the reference isn’t a universal reference.

    So IMHO there’s really no need for new syntax, it fits nicely into template argument deduction and reference collapsing rules, with the small tweak that a template parameter can be deduced as a reference type (where in C++03 a function template parameter of type T or T& would always deduce T as an object type.)

    These semantics and this syntax were proposed right from the beginning when rvalue references and a tweak to the argument deduction rules were proposed as the solution to the forwarding problem, see N1385. Using this syntax to provide perfect forwarding was proposed in parallel with proposing rvalue references for the purposes of move semantics: N1377 was in the same mailing as N1385. I don’t think an alternative syntax was ever seriously proposed.

    IMHO an alternative syntax would actually be more confusing anyway. If you had template<typename T> void bar(T&@) as the syntax for a universal reference, but the same semantics as we have today, then when calling bar(i) the template parameter T could be deduced as int& or int and the function parameter would be of type int& or int&& … neither of which is “T&@” (whatever that type is.) So you’d have grammar in the language for a declarator T&@ which is not a type that can ever exist, because it actually always refers to some other type, either int& or int&&.

    At least with the syntax we’ve got the type T&& is a real type, and the reference collapsing rules are not specific to function templates using universal references, they’re completely consistent with the rest of the type system outside of templates:

    struct A {} a;
    typedef A& T;
    T&& ref = a;    // T&& == A&
    

    Or equivalently:

    struct A {} a;
    typedef A& T;
    std::add_rvalue_reference<T>::type ref = a;    // type == A&
    

    When T is an lvalue reference type, T&& is too. I don’t think a new syntax is needed, the rules really aren’t that complicated or confusing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a function, taking an rvalue reference: void whatever (std::unique_ptr<int>&&) {
How do I specialize a function template that takes a universal reference parameter? foo.hpp:
gmock does not support rvalue reference as parameter of mocked function ( issue report
If I define a function which accepts an rvalue reference parameter: template <typename T>
According to another answer , an rvalue reference will not extend the lifetime of
I'm learning about rvalue references, and the tutorial told me this: X foo(); X
I have 3 questions: Can I bind a lvalue directly to a rvalue reference?
In move.h, there're two overloads of forward template<typename _Tp> constexpr _Tp&& forward(typename std::remove_reference<_Tp>::type& __t)
Edit: To anyone reading this question for future use: the error had not to
This code below allows me to find the word error in all my files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.