2006-11-02 20:25

A a = A( b ) in C++

If you ever get involved in an argument whether A a = A( b ); is or is not equivalent to A a( b ); in C++ (like me yesterday ;-) ), the answer is "it depends". Eg. g++ has a switch for that, see the man page: -fno-elide-constructors: The C++ standard allows an implementation to omit creating a temporary which is only used to initialize another object of the same type. Specifying this option disables that optimization, and forces G++ to call the copy constructor in all cases.

So - in g++ by default it really means A a( b );, but it could also mean a copy construction from temporary [something like A a( A( b ) ); which you cannot really use unless b is a constant, eg. 3, or "blah" - would mean a declaration of a function named a otherwise] with -fno-elide-constructors. Try it yourself...


Posted by kendy | Permalink | Categories: OpenOffice.org