r/cpp • • 2d ago

NRVO from compilers' perspective

https://thisnotes.dev/blog/nrvo/

Hey. I've recently presented at Meeting C++ 2026 with a talk about NRVO. While you wait for the recording being available, you can check text version of the talk on my blog.

Also I have newsletter about C++, performance and backends: https://thisnotes.substack.com/

26 Upvotes

4 comments sorted by

3

u/UnusualPace679 1d ago

There are a few other cases when our optimization doesn’t work. For instance, when we can’t control a variable’s location in memory:

static S x;
S f() { return x; }

The example is bogus as it doesn't use the function f. Even if it does, the effect depends on whether S is non-trivial for the purposes of calls.

Or with structured bindings:

struct Compose { S s; int i; };
Compose Get() { return {S(), 1}; } // def

S f() {
    auto [s, i] = Get();
    return s; // cp
}

f() code is eventually equivalent to:

S f() {
    Compose __c = Get();
    S s = __c.s;
    int i = __c.i;
    return s;
}

Should be S& s = __c.s; not S s = __c.s;. As the name suggests, structured bindings are name bindings for components of an object. That is, within f(), s is just a name of __c.s, not a new object. (Though technically, bindings are not references either. If you apply decltype, you'll get the referenced type.)

1

u/thisnotes 19h ago

Thanks for your comment. I think I've made the same typo with structured bindings in presentation itself and forgot to fix the article also.

About the first part. Of course usage of f() is implied in that example. I don't think I need to write a redundant line everywhere if it's understandable. But the part about ABI and returning pointer sometimes is useful. I'll learn a bit more about that and fix the article later. Thanks!

2

u/TheoreticalDumbass :illuminati: 23h ago

i think i would say bindings ARE references, but we lie about it in decltype

1

u/UnusualPace679 10h ago

Structured bindings can name bit-fields though, unlike references.