C++ unexpected behaviour (where are my temporaries!?) -
this r-value experiment mutated when gcc whined me lack of move-constructor (i'd deleted it) , didn't fall-back copy constructor (as expected) removed -std=c++11 flags , tried see below, has lot of output (it didn't initially) because trying work out why doesn't work (i know how debug find messages on stdout indicator of happening)
here's code:
#include <iostream> class object { public: object() { id=nextid; std::cout << "creating object: "<<id<<"\n"; nextid++; } object(const object& from) { id=nextid; std::cout << "creating object: "<<id<<"\n"; nextid++; std::cout<<"(object: "<<id<<" created object: "<<from.id<<")\n"; } object& operator=(const object& from) { std::cout<<"assigning "<<id<<" "<<from.id<<"\n"; return *this; } ~object() { std::cout<<"deconstructing object: "<<id<<"\n";} private: static int nextid; int id; }; int object::nextid = 0; object test(); int main(int,char**) { object a; std::cout<<"a ought exist\n"; object b(test()); std::cout<<"b ought exist\n"; object c = test(); std::cout<<"c ought exist\n"; return 0; } object test() { std::cout<<"in test\n"; object tmp; std::cout<<"test's tmp ought exist\n"; return tmp; }
output:
creating object: 0 ought exist in test creating object: 1 test's tmp ought exist b ought exist in test creating object: 2 test's tmp ought exist c ought exist deconstructing object: 2 deconstructing object: 1 deconstructing object: 0
i use deconstructing, because deconstruction word, use destructor, i'm never quite happy word, favour destructor noun.
here's expected:
a constructed tmp in test constructed, temporary created tmp, tmp destructed(?) temporary argument b's copy constructor temporary destructed. c's default constructor used "" temporary `test` c's assignment operator used temporary destructed c,b,a destructed.
i have been called "die-hard c" , trying learn use c++ more "c namespaces".
someone might "the compiler optimises out" i'd person never answer question such answer or ever, optimisations must not alter program state, must as if happened specification says, compiler may humor me putting message on cout includes number, may not bother increase number , such, output of program same if did code describes.
so it's not optimisations, what's going on?
it optimization, 1 allowed alter observable behaviour of program.
here's paragraph 12.8./31
, taken standard draft n3337 (emphasis mine):
when criteria met, an implementation allowed omit copy/move construction of class object, if copy/move constructor and/or destructor object have side effects. in such cases, implementation treats source , target of omitted copy/move operation 2 different ways of referring same object, , destruction of object occurs @ later of times when 2 objects have been destroyed without optimization. elision of copy/move operations, called copy elision, permitted in following circumstances (which may combined eliminate multiple copies):
— in return statement in function class return type, when expression name of non-volatile automatic object (other function or catch-clause parameter) same cv- unqualified type function return type, copy/move operation can omitted constructing automatic object directly function’s return value
— in throw-expression, when operand name of non-volatile automatic object (other function or catch-clause parameter) scope not extend beyond end of innermost enclosing try-block (if there one), copy/move operation operand exception object (15.1) can omitted constructing automatic object directly exception object
— when temporary class object has not been bound reference (12.2) copied/moved class object same cv-unqualified type, copy/move operation can omitted constructing temporary object directly target of omitted copy/move
— when exception-declaration of exception handler (clause 15) declares object of same type (except cv-qualification) exception object (15.1), copy/move operation can omitted treating exception-declaration alias exception object if meaning of program unchanged except execution of constructors , destructors object declared exception-declaration.
[example... omitted]
the semantics of copy/move constructor that, copying/moving contents of object while initializing one. if copy constructors send emails invitations birthday party should not surprised if end partying alone :)
ok, copy constructors other things, too. think reference counting of smart pointer. if gets optimized away, it's fine. there no copy , nothing needed counted.
Comments
Post a Comment