www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - slight correction for exception safety?

reply Karen Lanrap <karen digitaldaemon.com> writes:
From the article:

class Foo
{
    bool verbose;	// true means print messages, false means 
silence
    ...
    bar()
    {
	auto verbose_save = verbose;
	verbose = false;
	scope(exit) verbose = verbose_save;

	...lots of code...
    }
}

The scope statement should be moved before the assignment.

By the way: what will happen if a scope statement is used as an 
argument for a lazy parameter of a function?
Oct 15 2006
next sibling parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Karen Lanrap wrote:
 By the way: what will happen if a scope statement is used as an 
 argument for a lazy parameter of a function?

Arguments to functions are expressions and not statements, so this cannot happen.
Oct 15 2006
prev sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
In the specific case named, I'm not sure it matters; is an exception 
really going to happen between those two lines?  If it could, it's just 
as likely one could happen inside the scope statement itself, nay?

I think it would increase clarity to change it as you suggest, but it 
was done this way to compare it with the example above it with as little 
changes as possible to the code flow (I assume.)

And, actually, bar() should really have a return type - shouldn't it? 
At least void, or something.

As for using it as a lazy parameter; it would build a delegate, and so 
the code would always be run.  Consider:

function foo()
{
    function dg()
    {
       scope (exit)
          writefln("Exited dg's scope.");
    }

    dg();
}

In the above, it is not unclear what will happen.  A lazy parameter 
simply constructs a delegate, and as such would work exactly the same 
way (being that the lazy expression has its own scope.)

I agree that this is not immediately clear.  In my opinion, scope 
statements should be disallowed in lazily evaluated expressions (if they 
aren't already) because of this lack in clarity.

-[Unknown]


 From the article:
 
 class Foo
 {
     bool verbose;	// true means print messages, false means 
 silence
     ...
     bar()
     {
 	auto verbose_save = verbose;
 	verbose = false;
 	scope(exit) verbose = verbose_save;
 
 	...lots of code...
     }
 }
 
 The scope statement should be moved before the assignment.
 
 By the way: what will happen if a scope statement is used as an 
 argument for a lazy parameter of a function?

Oct 15 2006