Skip to content

NRVO translation is not NRVO eligible #620

@rmccampbell

Description

@rmccampbell

When given a function invoking NRVO, for example:

C f() { C c; return c; } 

The insight returns

C f() { C c = C() /* NRVO variable */; return C(static_cast<C &&>(c)); } 

If I substitute this directly in the code however, the compiler can no longer apply NRVO and instead calls the move constructor on c. See https://godbolt.org/z/qecYW1e9e. When returning c directly, there is no move constructor and only one destructor called.

Note however that if c is a parameter then the move construction is necessary, so this seems to be a correct translation in that case, e.g.

C f(C c) { return c; // return C(static_cast<C &&>(c)); } 

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions