KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
At work we have a class with an expensive constructor so we would like it to be called as few times as possible. We looked through the uses of it and tried to make the code more RVO friendly so to say. However we found a quirk in the g++ compiler where we didn't understand what happened. Please consider the two implementations of operator+ const Imaginary Imaginary::operator+(const Imaginary& rhs) const { Imaginary tmp(*this); tmp.append(rhs); return tmp; } and const Imaginary Imaginary::operator+(const Imaginary& rhs) const { return Imaginary(*this).append(rhs); } I have put print-outs in the the various constructors and with the following little program int main(int argc, char* argv[]) { Imaginary x(1, 1); Imaginary y(2, 1); Imaginary c = x + y; return 0; } I get this print out with the first implementation of operator+ int/int ctor int/int ctor Copy ctor And I get the following when the second variant of operator+ is in use int/int ctor int/int ctor Copy ctor Copy ctor Here we see that g++ is able to optimize away one call to the copy constructor in one case but not the latter and to my surprise, it managed to do it with the more clumsy implementation where I saved it to a temporary. Now I could've understood it more if it was the other way around but appearantly it isn't and now I am hoping that maybe one you could enlighten me on this subject. I should probably add that when we add --no-elide-constructors as a flag to g++ I get the following print out int/int ctor int/int ctor Copy ctor Copy ctor Copy ctor Regards, Mattias
Tags (comma-separated)
Save Edits
Cancel