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
4
votes
2 answers

How to move temporary object without std::move

Move constructor of class accepts rvalue reference which can be reference to temporary object. So, i have temporary object and appropriate move constructor which can accept reference to temporary object, but move constructor does not called. What`s…
Kri
  • 111
  • 2
  • 9
4
votes
2 answers

How to disable instantiating a temporary class?

I'm working with an expression template class which should not be instantiated to avoid dangling references. But I'm temped to declare a variable with auto and 'auto' create a named instance of a temporary class. How can I disable auto declaration…
4
votes
2 answers

copy initialization : why move or copy constructor was not called even if copy-elision is turned off?

My question is different because I may "know" copy-elision. I am learning copy initialization. However,the following code confused me because I have already turned off the copy-elision using -fno-elide-contructors -O0 option. #include…
4
votes
2 answers

Show where temporaries are created in C++

What is the fastest way to uncover where temporaries are created in my C++ code? The answer is not always easily deducible from the standard and compiler optimizations can further eliminate temporaries. I have experimented with godbolt.org and its…
Patrick Fromberg
  • 1,313
  • 11
  • 37
4
votes
1 answer

How to force the call to move constructor and why should I do that?

I tested this code to see that the compiler automatically transfers temporary object to variables without needing a move constructor. #include using namespace std; class A { public: A() { cout<<"Hi from default\n"; …
Nix
  • 351
  • 5
  • 13
4
votes
1 answer

Returning a copied object

In a function like: template A simple_return(Iterator it) { return *it; } A a = simple_return(my_it); The compiler can easily perform a RVO, so do that: template A simple_return(Iterator it) { A tmp = *it; …
ABu
  • 10,423
  • 6
  • 52
  • 103
4
votes
1 answer

C++11 copy elison and exception (catch argument)

After a code review we had an issue with copy elison in try/catch block. After reading this page : cpp reference guide and particularly this paragraph : When handling an exception, if the argument of the catch clause is of the same type (ignoring…
clam37
  • 143
  • 9
4
votes
1 answer

C++11 tuple with copy elision or move semantic

I wrote a function like below: template std::tuple, T, T> f() { std::vector p(1000); return std::make_tuple(std::move(p), 10, 10); } Since the return type is quite complicated, is it guaranteed that under…
Huy Le
  • 416
  • 4
  • 6
4
votes
4 answers

Pointers to stack-allocated object and move-contruction

Note: This is a complete re-wording of a question I posted a while ago. If you find they are duplicate, please close the other one. My problem is quite general but it seems that it could be explained more easily based on a concrete simple example.…
Bérenger
  • 2,678
  • 2
  • 21
  • 42
4
votes
1 answer

How to disable RVO in C++ Builder (Clang)?

I'm using C++ Builder XE6, and i'm trying to do some tests with move constructor, to compare the results with copy constructor. Anyway, my compiler forces RVO optimization even though it's checked "Disable all optimizations" in the compiler…
Tracer
  • 2,544
  • 2
  • 23
  • 58
4
votes
3 answers

Will compilers apply move semantics automatically in a setter method?

I want to know if the compiler is allowed to automatically use the move constructor for wstring in the following setter method (without an explicit call to std::move): void SetString(std::wstring str) { m_str = str; // Will str be moved into…
3
votes
1 answer

Copy/move elision vs emplace with std::optional

I was using an std::optional to store a struct, and I initially went with: std::optional optPosition; // declared somewhere as a class member optPosition.emplace(..., ..., ...); works great, but I don't really like that you have to define…
fab
  • 55
  • 4
3
votes
1 answer

Why may this code disable move semantics and copy elision?

Sometimes we may defer perfect returning like this: template decltype(auto) call(Func f, Args&&... args) { decltype(auto) ret{f(std::forward(args)...)}; // ... return…
o_oTurtle
  • 1,091
  • 3
  • 12
3
votes
1 answer

Why doesn't "Guaranteed Copy Elision" mean that push_back({arg1, arg2}) is the same as emplace_back(arg1, arg2)?

Firstly, I've heard that Guaranteed Copy Elision is a misnomer (as, I currently understand, it's more about fundamentally redefining the fundamental value categories, r/l-values to l/x/pr-values, which fundamentally changes the meaning and…
Elliott
  • 2,603
  • 2
  • 18
  • 35
3
votes
0 answers

Can copy elision be achieved for non-aggregates in member-initializers?

Is there any way to ensure members of a non-aggregate type are initialized with no copy and no move, i.e. using in-place construction? Suppose I have a heavy type #include #include #include struct S { std::string…
mCoding
  • 4,059
  • 1
  • 5
  • 11