不写了,这个东西越研究就越多,滚雪球一样,暂时撤退。。
感觉现在C++水平也算是勉强入门了,不再是C with STL了(逃)。
不过很多东西诸如compiling, parsing, linking and symbol table, etc
都慢慢有了一些细微的认知。。。
参考来源: (主要3\4\5)
[1].http://zh.cppreference.com/w/cpp/language/copy_elision
[2].http://blog.csdn.net/PeerlessBloom/article/details/77096198
[3].https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/RVO_V_S_std_move?lang=en
[4].https://stackoverflow.com/questions/48160292/in-c11-does-returning-a-stdstring-in-a-function-move-or-copy-it
[5].https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization/12953129#12953129
[6].https://stackoverflow.com/questions/4986673/c11-rvalues-and-move-semantics-confusion-return-statement?lq=1
[7].https://msdn.microsoft.com/zh-cn/library/ms364057(v=vs.80).aspx
From [6] we may take a little cognizance of rvalue and move semantics, and remember:
The following four figures are from [2], (Mr.peerlessbloom)
(but I do not know how to remove the watermark,
apologize here…………………………………………………………………………..!!!!!!!!)
…………………………………………………………………………………………………………………..
Copy elision is an optimization implemented by most compilers to prevent extra (potentially expensive) copies in certain situations. It makes returning by value or pass-by-value feasible in practice (restrictions apply).
When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the copy/move constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization. This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):
1) in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value.
2) in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object can be omitted by constructing the automatic object directly into the exception object.
3) when a temporary class object that has not been bound to a reference would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move
4) when the exception-declaration of an exception handler declares an object of the same type (except for cv-qualification) as the exception object, the copy/move operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.
Ex.
A MyMethod (B &var)
{
A retVal;
retVal.member = var.value + bar(var);
return retVal;
}
valA = MyMethod(valB);
VC++ compiler may have optimize the code and it may have the following structure:
A MyMethod (A &_hiddenArg, B &var)
{
A retVal;
retVal.A::A(); // constructor for retVal
retVal.member = var.value + bar(var);
_hiddenArg.A::A(retVal); // the copy constructor for A
return;
retVal.A::~A(); // destructor for retVal
}
…………………………………………………………………………………………………………………….
(N)RVO : (Named) Return Value Optimization
RVO, is a compiler optimization technique that allows the compiler to construct the return value of a function at the call site.
The mechanism of RVO:
This diagram is a normal function stack frame. If we call the function without RVO, the function simply allocates the space for the return in its own frame. The process is demonstrated in the following diagram:
What will happen if we call the function with RVO?
We can find that RVO uses parent stack frame (or an arbitrary block of memory) to avoid copying. So, if we add if-else branch, the compiler doesn’t know which return value to put.
太多了,到此为止。