Questions tagged [copy-elision]

Copy elision refers to an exception to the as-if rule allowing to omit copies

Copy Elision is an exception to the as-if rule governing the behavior of C++ programs.

Return-value-optimization is copy-ellision applied to value-returns.

12.8 Copying and moving class objects [class.copy]

31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.123
This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

  • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value
  • in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object (15.1) can be omitted by constructing the automatic object directly into the exception object
  • when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move
  • when the exception-declaration of an exception handler (Clause 15) declares an object of the same type (except for cv-qualification) as the exception object (15.1), the copy operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.
272 questions
2
votes
3 answers

lvalue And rvalue Return Types For Efficiency

Update: So, as per the comment by Jean, I have looked at this link. This is actually pretty close to the question I am asking. The only difference is that it deals with Classes explicitly (which cv::UMat undoubtedly is); However, would the…
9301293
  • 514
  • 3
  • 16
2
votes
1 answer

Are C++ compilers allowed to replace construct + moveconstruct with just a construct?

Are C++ compilers allowed to replace: const auto myType = MyType(1, 2, 3); with: const MyType myType(1, 2, 3); ie, emit the assignment, or is there anything which prevents this? Note: The reason I ask is that I prefer the first version.
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
2
votes
0 answers

chained copy elision with noncopyable types

Consider the following example: #include #include #include struct X { boost::optional> foo(int i) { std::unique_ptr t(new int{i}); return t; // every…
Barry
  • 286,269
  • 29
  • 621
  • 977
2
votes
2 answers

Why is object copy constructed and destructed twice?

Why does an "extra" pair of copy constructor and destruction occur in code below? It happens when the constructor of Dingledong takes an STL container as an argument (I've tried std::vector and std::list). It may happen for other things? It doesn't…
2
votes
1 answer

Do implicit class-type conversions use the copy-constructor?

The following quote from my C++ book: When we use direct initialization, we are asking the compiler to use ordinary function matching to select the constructor that best matches the arguments we provide. When we use copy initialization, we are…
2
votes
3 answers

Return object that cannot be copied by value

I have a object whose copy operation would be too slow so I decided to delete it and force users to only move. A copy of this object wound't make much sense anyway. But then I have this function: Object loadFromFile(const std::string& name) { …
Guilherme Bernal
  • 8,183
  • 25
  • 43
2
votes
1 answer

Template class copy constructor not called

My copy constructor is not being called and I'm not sure why. Here's my code: template class SmartPtr { public: explicit SmartPtr(T *p) : m_p(p) { cout << "ctor" << endl; } SmartPtr(const SmartPtr& p) : m_p(p.m_p) {…
Syed H
  • 305
  • 1
  • 2
  • 9
2
votes
1 answer

C++11 constructor argument: std::move and value or std::forward and rvalue reference

Which of the below two should be preferred and why? struct X { Y data_; explicit X(Y&& data): data_(std::forward(data)) {} }; vs struct X { Y data_; explicit X(Y data): data_(std::move(data)) {} };
bobah
  • 18,364
  • 2
  • 37
  • 70
2
votes
1 answer

Copy/move elision versus explicitly deleted copy/move constructors

I want to know when copy/move elision applies (or is allowed to apply) to explicitly deleted copy/move constructors and to non-deleted copy/move constructors. Here are the specifics: Can an explicitly deleted copy ctor or move ctor get elided? Is…
CodeBricks
  • 1,771
  • 3
  • 17
  • 37
2
votes
3 answers

Eliminating copying of function parameter

I am writing a custom memory allocator. If possible, I want to make object creation function like this to abstract creation procedure completely. template class CustomCreator { virtual T& createObject(T value)…
eonil
  • 83,476
  • 81
  • 317
  • 516
2
votes
1 answer

Is this copy constructor elision?

The following code doesn't call the copy constructor. struct X { int x; X(int num) { x = num; std::cout << "ctor" << std::endl; } X(const X& other) { std::cout << "copy ctor" << std::endl; } }; int main(int…
AMCoder
  • 773
  • 1
  • 6
  • 15
1
vote
0 answers

Add members at compile time without copy/move constructors

I'm learning about Variadic Template and Fold Expression. I would like to avoid the use of dynamic allocation and pointers. To illustrate my problem, I created the Foo (copy and move constructors are deleted) class which inherits from I_Foo. class…
1
vote
1 answer

Number of copies when -fno-elide-constructors flag is passed to the compiler

Consider the following class: struct IntPointerWrapper { int* data_pointer; IntPointerWrapper() : data_pointer(new int()) { std::cout << "IntPointerWrapper()" << std::endl; } IntPointerWrapper(const IntPointerWrapper&…
Luckk
  • 518
  • 3
  • 7
1
vote
1 answer

Is it possible to in-place initailize std::variant given a factory object and variadic arguments list?

std::variant has a constructor that accepts std::in_place_t, Args &&... arguments that results in in-place construction. { std::cout << "in_place_type:\n"; const auto var =…
Sergey Kolesnik
  • 3,009
  • 1
  • 8
  • 28
1
vote
1 answer

Is move elision guaranteed in this case?

Consider the following C++20 code; assume T to be non-movable and non-copyable: struct Cell { Cell(T&& instance) : obj(std::move(instance)) {} private: T obj; }; Cell cell(T{/* arguments */}); Is move elision guaranteed in the constructor…
Pavel Kirienko
  • 1,162
  • 1
  • 15
  • 31