Scope guards

Steven Schveighoffer schveiguy at gmail.com
Tue Jun 27 02:12:41 UTC 2023


On 6/26/23 2:42 PM, Cecil Ward wrote:

> ==
> {
> size_t p = offset;
> ++p;
> scope(exit) { writeOutput( 0, p );
> 
> ++p
>> ++p;
> 
> return;
> }
> 
> ==
> 
> The correctness of its behaviour depends on what the value of p is when 
> it calls writeOutput(), and the value of p is being changed. To be 
> correct, the final value of p needs to be passed to writeOutput( p ). 
> That was what I was worrying about. I could have course introduce 
> another variable to capture this final value and use that in the scope 
> guard, but then I can’t make the scope guard general if I have more than 
> one exit route. The compiler sounded as if it did not like the local 
> variable p in the scope guard, but I need to try it again to get the 
> error message.

That will print the final value of `p`. `p` lives at the same address 
for the entire function, and the scope exit prints the value from that 
address.

-Steve


More information about the Digitalmars-d-learn mailing list