WTF did happen with struct constructor and ref in 2.061 ?

js.mdnq js_adddot+mdng at gmail.com
Fri Jan 4 12:47:42 PST 2013


On Friday, 4 January 2013 at 16:47:38 UTC, Jonathan M Davis wrote:
> On Friday, January 04, 2013 13:12:52 js.mdnq wrote:
>> can you show an example of such a bug? I assume you mean that a
>> "struct literal" ends up being a local object and a ref to it 
>> can
>> easily become invalid?(in your example this is not possible
>> inside main). But your example basically contradicts what you 
>> say.
>> 
>> There is no semantic difference between
>> 
>> S s = S(2); foo(s)
>> 
>> and
>> 
>> foo(S(2));
>
> There's a _huge_ difference between those two. In the first 
> case, you have a
> variable which exists beyond the end of the function call. In 
> the second, you
> have temporary which is destroyed as soon as the statement has 
> completed.

Nope, sorry, it's not. That is only a compiler optimization. Just 
because the compiler decides to do something to make them 
different does not mean they are. The compiler could easily just 
make S(2) null for no obvious reason because it wants to.

But if the compiler decides to make a different because of some 
reason(such as optimization then it also can decide to not do so.

For example, tell me why the compiler can't just expand

    foo(S(2));

to

    S ___s = S(2);
    foo(___s)

where ___s is hidden?

Then there is obviously no difference. But nothing is stopping 
the compiler from doing so and in fact, it could be doing so 
already unless you know it's not. If it did do such a hidden 
expansion then you would agree that the two cases are identical? 
(if not then there is no point is discussing any further because 
we can't even agree on basic logic)

Now, if the compiler decides to optimize the first case 
differently then the second case, that is not because they are 
different but because who ever write the code thinks they are.

Just because something looks different from something else does 
not mean they are.


More information about the Digitalmars-d mailing list