Reference of Reference in C++

后端 未结 3 1712
别那么骄傲
别那么骄傲 2020-12-04 11:22

I see code on StackOverflow every once in a while, asking about some overload ambiguity with something involving a function like:

void foo(int&& para         


        
3条回答
  •  天命终不由人
    2020-12-04 11:48

    It's a rvalue reference, Bjarne describes it here.

    Shameless copying ("quoting"):

    The rvalue reference

    An rvalue reference is a compound type very similar to C++'s traditional reference. To better distinguish these two types, we refer to a traditional C++ reference as an lvalue reference. When the term reference is used, it refers to both kinds of reference: lvalue reference and rvalue reference.

    An lvalue reference is formed by placing an & after some type.

    A a; A& a_ref1 = a;  // an lvalue reference
    

    An rvalue reference is formed by placing an && after some type.

    A a; A&& a_ref2 = a;  // an rvalue reference
    

    An rvalue reference behaves just like an lvalue reference except that it can bind to a temporary (an rvalue), whereas you can not bind a (non const) lvalue reference to an rvalue.

    A&  a_ref3 = A();  // Error! 
    A&& a_ref4 = A();  // Ok
    

提交回复
热议问题