Reference value of structs not optimized or inlined?

Jeremie Pelletier jeremiep at gmail.com
Thu Aug 27 19:32:52 PDT 2009


Walter Bright Wrote:

> Jeremie Pelletier wrote:
> > Isn't there a way to implement RVO to work on parameters (PVO?) too
> > if the storage is const?
> 
> No, and it doesn't work for C++ either. Consider:
> 
> struct S { int a; }
> 
> void foo(const ref S s)
> {
>      assert(s.a == 3);
>      bar();
>      assert(s.a == 3); // OOPS!
> }
> 
> S g;
> 
> void bar()
> {
>      g.a = 4;
> }
> 
> void main()
> {
>      foo(g);
> }

Yes but that would be a logic error from the programmer, as it would be possible to do the very same thing had foo been declared as void foo(in S* s).

Isn't it possible to make 'const ref S' or 'in S' generate the same machine code as 'in S*'? To me it would seem the semantics of the two are the same, with 'const S*' being useful syntax for C compatibility while 'in S' and 'const ref S' are both D syntax.

I don't use ref and out whatsoever in D because of their overhead and inability to inline, which is sad because the first time I read about D I pictured having every single parameter in my programs having at least one of in, ref or out and finally saying bye bye to pointers.



More information about the Digitalmars-d mailing list