Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

c++ - Conditions for copy elision?

I wanted to verify the if the following optimizations work as expected:

  • RVO
  • Named RVO
  • Copy elision when passing an argument by value

So I wrote this little program:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <vector>

struct Foo {
    Foo(std::size_t length, char value) : data(length, value) { }

    Foo(const Foo & rhs) : data(rhs.data) { std::cout << "*** COPY ***" << std::endl; }

    Foo & operator= (Foo rhs) {
        std::cout << "*** ASSIGNMENT ***" << std::endl;
        std::swap(data, rhs.data); // probably expensive, ignore this please
        return *this;
    }

    ~Foo() { }

    std::vector<char> data;
};

Foo TestRVO() { return Foo(512, 'r'); }

Foo TestNamedRVO() { Foo result(512, 'n'); return result; }

void PassByValue(Foo inFoo) {}

int main()
{
    std::cout << "
Test RVO: " << std::endl;
    Foo rvo = TestRVO();

    std::cout << "
Test named RVO: " << std::endl;
    Foo named_rvo = TestNamedRVO();

    std::cout << "
Test PassByValue: " << std::endl;
    Foo foo(512, 'a');
    PassByValue(foo);

    std::cout << "
Test assignment: " << std::endl;
    Foo f(512, 'f');
    Foo g(512, 'g');
    f = g;
}

And I compiled it with optimizations enabled:

$ g++ -o test -O3 main.cpp ; ./test

This is output:

Test RVO: 

Test named RVO: 

Test PassByValue: 
*** COPY ***

Test assignment: 
*** COPY ***
*** ASSIGNMENT ***

According to the output RVO and named RVO work as expected. However, copy elision is not performed for the assignment operator and when calling PassByValue.

Is copy elision not allowed on user defined copy-constructors? (I know that RVO is explicitly allowed by the standard but I don't know about copy elision when passing by value.) Is there a way to verify copy elision without defining copy constructors?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The standard says (in paragraph 12.8.15):

This elision of copy operations 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 with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value

  • when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the tempo- rary object directly into the target of the omitted copy

Neither of these cases applies here, so the elision is not allowed. The first on is obvious (no return). The second is not allowed, because the object you pass in is not a temporary.

Note that your code is still fine, because you would have to create the copy anyway. To make away with that copy, you would have to use C++0x's move-semantics.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...