Hello there, can you please explain to me when a move constructor/assignment
is called and what exactly happens when so.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string use(string& a)
{
string tmp{ a };
if (tmp.size()) tmp[0] = 'A';
return tmp;
}
void f()
{
string str = "Hello";
string k = use(str); // The move assignement is called here right?
int a = 5;
int b = static_cast<int&&>(a); // and here?
}
Line 11: Yes, the move constructor (not assignment) is called here, but I must point out that it's called after use() returns, not before. In other words, the string that's moved is the return value of use(), not str.
Line 13: I'm unclear what you're asking here. Yes, int::int(int &&) will be called, but the semantics of move constructors for PODs is identical to that of copy constructors. What I mean is that at line 14 a == 5; the value will be copied, not actually moved.
Also, generally one uses std::move() to explicitly request move construction and assignment, rather than static_cast.
EDIT: Missed Peter87's post earlier; leaving this post here because it has a snippet.
> The move assignement is called here right?
The copy constructor is invoked on line 3 in function use (to create the object tmp)
Copying of the local object with automatic storage duration is typically elided,
at least when optimisations are enabled (NRVO: named return value optimisation).
Note: with the Microsoft compiler, disable optimisations (compile without the -Og option), and the local object with automatic storage duration is move constructed (NRVO is not applied). http://rextester.com/DNMF78403