Hello,

Sorry if this question is a bit naive or shows a misunderstanding of RVO. I was trying to see if my C compiler was doing RVO with struct, but after testing it at is apparently not the case.

Since I have heard that D supports RVO I wanted to give it a try in D. But apparently it doesn't do RVO as I expected it would do it.

Here is the D code. It is very similar to the C code I tested with gcc and clang :
---
#!/usr/bin/rdmd -O

import std.stdio;

struct S { int a, b; };

S foo(S* p) {
    S v = {1, 2};
    writeln("foo: return value optimization: ", p == &v);
    return v;
}

void main()
{
    S x;
    x = foo(&x);
}
---

My assumption is the following. x is the target variable where to store the result of foo. I expected that with RVO foo() would receive the address where to store its result as hidden argument.

Inside foo(), the optimizer would detect that v, its returned value, will be stored at the location given as hidden argument. It may then optimize out v so that foo() uses x as storage for v.

My code is testing if this is the case and it is apparently not.

Can someone please explain me why this doesn't work as I would expect RVO to work ?

Reply via email to