If I have T && temp = std :: move (other); then use this for a function that takes the value T by value

so let's say I have the following function:

void foo(std::string strParam) // pass-by-value
{
    // function-body
}

therefore strParam foo (string) will either be created using a copy (if arg was an lvalue) or move (if arg was an rvalue).

as everyone knows

foo("blah"); // rvalue; so string move constructor invoked for strParam.

against

string bar = "blah";
foo(bar); // lvalue; so string copy constructor invoked for strParam.

again,

string bar = "blah";
foo(move(bar)); // xvalue; so move constructor.

and for the reference variable named rvalue

string &&temp = // can be whatever
foo(temp); // *named* rvalue reference IS a lvalue; so copy constructor.

so I guess that means

string &&movedBar = move(bar);
foo(movedBar); // it actually invokes copy constructor.

therefore we call

foo(move(bar)) 

differs from

string&& movedBar = move(bar);
foo(movedBar)

because one of them is an unnamed rvalue (xvalue) link, and the other is called an rvalue (lvalue) link

right, right?

+4
source share
1 answer

One correction:

foo("blah"); // rvalue; so string move constructor invoked for strParam.

std::string, const char*, std::string. std::string - .

. :

foo("blah"); // calls string constructor that takes a const char*
foo(bar); // calls string copy ctor
foo(move(bar)); // calls string move ctor

string&& movedBar = move(bar);
foo(movedBar); // calls copy ctor

. , foo("blah") , foo(string("blah")). "blah", - strParam. , , , string strParam(string("blah")) . delete - -fno-elide-constructors.

, , . const char* (~ ish?).

+6

All Articles