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 9205939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:04:27+00:00 2026-06-18T00:04:27+00:00

Suppose we have: foo(A&& a); if you do A a; foo(a); it won’t compile

  • 0

Suppose we have:

foo(A&& a);

if you do

A a;
foo(a);

it won’t compile and complain cannot bind a lvalue to A&&. that’s perfectly fine.

However, given the signature of std::move,

template<class T> typename remove_reference<T>::type&& std::move(T&& a);

Looks like it takes a rvalue reference, just as in foo, why the following code complies?

A a;
std::move(a);

isn’t a is a lvalue?

furthur, it is said the compile will instantiate:

typename remove_reference<A&>::type&& std::move(A& && a);

I don’t understand why it is not:

typename remove_reference<A>::type&& std::move(A && a);

it looks to me a is of type A, not A&.

  • 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-18T00:04:28+00:00Added an answer on June 18, 2026 at 12:04 am

    Despite what others have said, the standard only talks about rvalue references.

    The key to how this works for std::move is an explicit special rule in the rules for template argument deduction:

    […] If [the declared function parameter type] is an rvalue reference
    to a cv-unqualified template parameter and the argument is an lvalue,
    the type “lvalue reference to A” is used in place of A for type
    deduction.[…]

    The other part are the rules for reference collapsing, which say that

    If […] a type template-parameter […] denotes a type TR that is a reference to
    a type T, an attempt to create the type “lvalue reference to cv TR”
    creates the type “lvalue reference to T”, while an attempt to create
    the type “rvalue reference to cv TR” creates the type TR.

    Now in template<class T> typename remove_reference<T>::type&& std::move(T&& a); the function parameter a matches above rule (“rvalue reference to cv-unqualified template parameter”), so the deduced type will be an lvalue reference to the argument type, if the argument is an lvalue. In your case that leads to T = A&.

    Substituting that into the declaration of move yields

    remove_reference<A&>::type&& std::move<A&>(A& && a);
    

    Using the definition of remove_reference and the reference collapsing rule (rvalue reference to TR => TR), makes this:

    A&& std::move<A&>(A& a);
    

    Scott Meyer’s universal reference concept, as put forward in other answers, is a helpful way to remember this surprising effect of the combination of the rules for type deduction and of reference collapsing: rvalue references to a deduced type may end up being lvalue references (if the type may be deduced to be a lvalue reference). But there are no universal references int the standard. As Scott Meyers says: it is a lie – but a lie that is more helpful than the truth…

    Note that std::forward is a different twist on this theme: it uses an extra indirection to prevent argument deduction (so that the type must be given explicitly), but also uses reference collapsing to forward lvalues as lvalues and rvalues as rvalues.

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

Sidebar

Related Questions

Suppose you have: template<class T> class A { template<class T1> void foo(const T1& t1)
Suppose I have method: void foo(const std::string& s); Can I create boost::function: boost::function<void(const std::string&)>
Suppose you have the following code: (ns foo) (defmacro defproject [project-name version & args]
Suppose I have a function template: template <typename T> std::string foo(const T& x) {
Suppose I have a class class Foo { public: ~Foo() { delete &_bar; }
Suppose I have this class: class foo { public: foo() { } foo(const std::string&
Suppose I have a method foo that is defined thus: void foo(MyObject a, MyObject
Suppose I have a class like this: class Foo { std::vector<int> bar; public: std::vector<int>&
For example, suppose I have a class: class Foo { public: std::string& Name() {
Let's suppose I have a bash script ( foo.sh ) that in a very

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.