are scope guards (scope(exit, success, failure)) zero-cost abstractions?

Timothee Cour thelastmammoth at gmail.com
Thu Feb 8 11:00:25 UTC 2018


I know that, my question is whether it adds any runtime overhead over
naive way (which is to call the "bar" finalizer before each return
statement)  in the case where no exception is thrown


On Thu, Feb 8, 2018 at 2:44 AM, Mike Parker via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:
> On Thursday, 8 February 2018 at 10:09:12 UTC, Timothee Cour wrote:
>>
>> I'm curious whether scope guards add any cost over the naive way, eg:
>>
>> ```
>> void fun(){
>>   ...
>>   scope(success) {bar;}
>>   ...
>> }
>> ```
>>
>> vs:
>>
>> ```
>> void fun(){
>>   ...
>>   if(foo1){
>>     bar;  // add this before each return
>>     return;
>>   }
>>   ...
>>   bar;
>>   return;
>> }
>> ```
>>
>> For scope(success) and scope(failure), the naive way would anyway
>> involve try/catch statements but what about scope(exit)? Does the
>> zero-cost exception model (zero cost being for non-thrown exceptions)
>> guarantee us that scope(success) has 0 overhead over naive way?
>
>
> Scope guards are lowered to the equivalent try/catch/finally blocks anyway.


More information about the Digitalmars-d-learn mailing list