Martin Ba
Martin Ba

Reputation: 38766

Is it possible to ensure copy elision?

Copy elision is a neat optimization technique and in some cases relying on copy elision can actually be faster than passing around references "by hand".

So, let's assume you have identified a critical code path where you rely on the fact that the copy elision is performed by your compiler for the code path for maximum performance.

But now you are relying on a compiler optimization.

Is there any (compiler specific, obviously) way to ensure that the copy elision is actually performed and have the compiler (or another tool) generate a warning/error if the copy elision cannot be performed?

(I'm thinking of something remotely similar to Visual C++'s __forceinline than will generate a warning if the function marked thus isn't inlined by the compiler.)

Upvotes: 7

Views: 1155

Answers (4)

thatsafunnyname
thatsafunnyname

Reputation: 176

Is there any (compiler specific, obviously) way to ensure that the copy elision is actually performed and have the compiler (or another tool) generate a warning/error if the copy elision cannot be performed?

gcc (trunk) (not yet released v14) has -Wnrvo

"Warn if the compiler does not elide the copy from a local variable to the return value of a function in a context where it is allowed by [class.copy.elision]. This elision is commonly known as the Named Return Value Optimization."

gcc (trunk) is available on https://godbolt.org/

Upvotes: 1

Phil Miller
Phil Miller

Reputation: 38118

In C++1z (expected 2017), some cases will be required to guarantee copy elision:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0135r0.html

Per the communal cppreference.com compiler feature support wiki GCC 7+ and Clang 4+ ensure this.

The optimization side of this fortunately should not require enabling newer language support, since it's a pure optimization (following older language standard allowances).

Also allowing the copy constructor to be unavailable when the optimization applies probably will require the newer language standard to be enabled during compilation, or use of a loose or extended mode that doesn't demand strict conformance (e.g. potentially GCC's -fpermissive).

Upvotes: 2

Bo Persson
Bo Persson

Reputation: 92231

Not really, except putting an assert(false); in the copy constructor.

Otherwise use your favorite profiler to measure that the interesting parts of your app is fast enough.

Upvotes: 4

Yakov Galka
Yakov Galka

Reputation: 72469

No.

But you can write an equivalent, although completely unreadable, code:

BigObj f()
{
    BigObj x(g());
    x.someMethod();
    return x;
}

//...
BigObj z = f();
//...

is translated (with copy elision) to:

void f(BigObj* obj)
{
    new(obj) BigObj(g());
    obj->someMethod();
}

//...
char z[sizeof(BigObj)];
f((BigObj*)&z[0]);
//...
((BigObj*)&z[0])->~BigObj();

But seriously, just write your code in such a way that the compiler can elide the copy. I.e. return only one object without branching:

BigObj f()
{
    BigObj x, y;
    // use x and y
    if(condition)
        return x;
    else
        return y;
    // cannot be elided
}


BigObj f()
{
    if(condition)
    {
        BigObj x;
        return x;
    }
    else
    {
        BigObj y;
        return y;
    }
    // can be elided
}

Upvotes: 4

Related Questions