Menu
  • HOME
  • TAGS

Return value optimization and copy elision in C

c++,c,struct,return-value-optimization,copy-elision

RVO/NRVO are clearly allowed under the "as-if" rule in C. In C++ you can get observable side-effects because you've overloaded the constructor, destructor, and/or assignment operator to give those side effects (e.g., print something out when one of those operations happens), but in C you don't have any ability to...

Too many destructors called on template classes (N)RVO optimization

c++,templates,c++11,copy-elision

(N)RVO can never introduce a discrepancy between the number of constructor and destructor calls. It's designed to make that principally impossible. The problem is with your code. According to the rules of the language, a constructor template is never used to produce a copy constructor. The copy constructor is never...

Copy elision in range based for loops

c++,c++11,copy-elision

Quite simply because there is no rule to allow copy elision in this case. It's generally only allowed when returning a value from a function, or when copy-initialising. In every other case, where a copy has side-effects (such as your I/O), copy elision is prohibited. It would be madness for...

Auto and copy elision

c++,c++11,auto,copy-elision

I guess you use libstdc++. It is currently not standard conformant in this regard, i.e. the iostreams do not have move constructors yet although they should have. It will be fixed in version 5: Runtime Library (libstdc++) ... Full support for C++11, including the following new features: ... movable and...

Template class copy constructor not called

c++,templates,copy-constructor,copy-elision

This is what is known as Copy Elision. It's a nice optimization where a copy clearly isn't necessary. Instead of effectively running the code: SmartPtr<int> __tmp(new int); SmartPtr<int> ptr4(__tmp); __tmp.~SmartPtr<int>(); The compiler can know that __tmp only exists to construct ptr4, and thus is allowed to construct __tmp in-place in...

Do RVO and copy elision only work within one compilation unit or not?

c++,copy-elision,rvo

Normally, yes, but in principle, using Link-Time-Optimization (-flto for GCC/Clang compilers and linkers) or Link-Time-Code-Generation (/LTCG and /GL for MSVC's compiler and linker), the compiler and linker can leverage their shared knowledge and perhaps inline code and elide copies. GCC's manual states: [...] this causes all the interprocedural analyses and...

copy elision visible side effect

c++,c++11,copy-elision

The whole point of singling copy elision out as a on optimization in specific cases is to allow eliding side effects of copy construction. That is, yes, it expected that copy elision happens despite the copy constructor and/or the destructor having side effects. If you don't want copy elision to...

Copy Elision Misunderstanding

c++,c++11,copy-elision

Yes your understanding is right. Your line of code (without copy eliding) is similar to int main() { { A temp = func1(); // 2nd copy func2(temp); // 3rd copy } } ...

Return object that cannot be copied by value

c++,copy-constructor,c++14,copy-elision

This one is wrong: Object&& loadFromFile(const std::string& name) { Object obj; ... return std::move(obj); } You are returning a reference to a local variable, and this is undefined behaviour. The object dies and what you return is a reference to your nose, so demons can come out of it. The...