Here is the complete program:
#include <iostream> using std::cout; using std::endl; using std::move; int count {0}; // global for monitoring class Triple { public: Triple() = default; // C++11 use default constructor despite other constructors being declared Triple(Triple&&) = default; Triple(const Triple& t) : // copy constructor Triple(t.mX, t.mY, t.mZ) { count++; } Triple(const int x, const int y, const int z) : mX{ x }, mY{ y }, mZ{ z } { } const Triple& operator +=(const Triple& rhs) { mX += rhs.mX; mY += rhs.mY; mZ += rhs.mZ; return *this; } int x() const; int y() const; int z() const; private: int mX{ 0 }; // c++11 member initialization int mY{ 0 }; int mZ{ 0 }; };
An interesting fact is that the copy constructor has a side effect, and there are two versions of operator+ .
Case 1
inline Triple operator+(const Triple& lhs, const Triple& rhs) { Triple left { lhs }; left += rhs; return left; }
Case 2
inline Triple operator+(Triple left, const Triple& rhs) { left += rhs; return left; }
Visual Studio 2015 gives the same print result, result 1 . However, gcc 4.8.4 gives 2 for case 1.
This summary of issuing answers โป says that "this is not a function parameter", which makes me assume that VS is wrong. It is right?
But why are formal parameter names handled specifically in this rule? Why is this not like any other local variable?
(I'm not saying that the optimizer will parse things depending on the calling conventions and in the light of a separate compilation of the caller and call-ee, but simply why this is not allowed.)
Edit: If you correctly print 1 , how does this compare with the elite rule?
Note โป: I found that this text was copied from ยง12.8 of paragraph 31 in the public N3690 .
c ++ copy-elision visual-c ++
Jdลugosz
source share