Returning reference: why this works?

Johan Engelen j at j.nl
Wed Mar 13 21:04:01 UTC 2019


On Wednesday, 13 March 2019 at 20:57:13 UTC, Denis Feklushkin 
wrote:
> import std.stdio;
>
> struct S { int x; }
>
> ref S func1(ref S i) // i is reference
> {
>     return i;
> }
>
> ref S func2(S i) // i is not reference
> {
>   return func1(i); // Works! Possibility to return reference to 
> local object i?

Indeed, you're invoking UB here. With compiler flag `-dip25` that 
code no longer compiles.

-Johan





More information about the Digitalmars-d-learn mailing list