Passing an rvalue reference to a const lvalue reference parameter

I am trying to understand the references to C ++ 11 rvalue and how to use them for optimal performance in my code.

Let's say we have a class Athat has a pointer to a large amount of dynamically allocated data.

It is also a method foo(const A& a)that does something with a class object A.

I want to prohibit calling the copy constructorA when the object Ais passed to the function foo, since in this case it will make a deep copy of the underlying heap data.

I tested passing lvalue links:

    A a;
    foo(a);

and passing the rvalue link:

    foo(A());

In both cases, the copy constructor was not called.

(Apple LLVM 5.1)? - ?

+3
1

. ( lvalue rvalue), . .

, , . . :

void foo(A a);

A , , lvalue rvalue.

, , :

void foo(A& a);
void foo(const A& a);
void foo(A&& a);
void foo(const A&& a);

, ( -) - , / , rvalue. const lvalue reference:

  • (, , ), (A). , lvalue, ( ), rvalue, .

  • , const lvalue reference (const A&). , , lvalue rvalue, . , , .

, - , const A& .

+11

All Articles