WTF did happen with struct constructor and ref in 2.061 ?

Maxim Fomin maxim at maxim-fomin.ru
Sat Jan 5 11:34:07 PST 2013


On Friday, 4 January 2013 at 19:15:03 UTC, Ali Çehreli wrote:
> On 01/04/2013 10:48 AM, deadalnix wrote:
> > On Friday, 4 January 2013 at 16:47:38 UTC, Jonathan M Davis
> wrote:
>
> >>> 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.
>
> Still, there is no semantic difference. I know this issue from 
> C++. At first I had embraced it but I am not so sure anymore. 
> The language is getting out of its way to protect the 
> programmer. A language like C++ that gives so much freedom to 
> do many other unsafe things...

Why do you think there is no semantic difference?

> Constructors and functions do not affect objects alone. However 
> frowned upon, a function that is called on an S may have 
> side-effects that are beyond the object itself. If foo() does 
> s.bar() and S.bar has a side-effect, I wouldn't care about the 
> changes on the rvalue object itself.

Even if one particular function makes some side effects and does 
not care by taking ref parameter that changes may be discarded, 
it is a source of bugs in general, when taking argument by ref 
means that passed object must be modified and changes need to be 
preserved.


More information about the Digitalmars-d mailing list