Stupid scope destruction question

Denis Shelomovskij verylonglogin.reg at gmail.com
Mon Sep 24 00:26:22 PDT 2012


20.09.2012 15:35, monarch_dodra пишет:
> On Thursday, 20 September 2012 at 09:31:45 UTC, Denis Shelomovskij wrote:
>> 20.09.2012 13:27, Denis Shelomovskij пишет:
>>> Is there any guaranties that `ScopeTemp` will not be destroyed before
>>> `f` call because it isn't used?
>>> ---
>>> ...
>>>     f(ScopeTemp(...).value);
>>> }
>>> ---
>>> According to http://dlang.org/struct.html#StructDestructor
>>> "Destructors are called when an object goes out of scope."
>>> So I understand it as "it will not be destroyed before scope exit even
>>> if not used". Is it correct?
>>>
>>> So the question is if `ScopeTemp`'s scope is `main` scope, not some
>>> possibly generated "temp scope" (don't now what documentation statements
>>> prohibit compiler from doing so).
>>>
>>>
>
> AFAIK, if the rules are the same in C++ (which they probably are), then:
> "Any object constructed during argument passing will remain valid for
> the duration of the call. It will go out of scope once the function has
> finished returning, and after the return value has itself gone out of
> scope and been destroyed."
>

Thanks, looks like D does have C++ behaviour here. But your last 
statement about return value is incorrect. More than that function call 
doesn't change anything.

Correct answers are here:
* `12.2 Temporary objects [class.temporary]` section of C++ standard
* 
http://stackoverflow.com/questions/2506793/c-life-span-of-temporary-arguments
* 
http://stackoverflow.com/questions/5459759/full-expression-boundaries-and-lifetime-of-temporaries

-- 
Денис В. Шеломовский
Denis V. Shelomovskij


More information about the Digitalmars-d-learn mailing list