'raii' for structs

BCS BCS at pathlink.com
Fri Oct 27 09:54:41 PDT 2006


David Medlock wrote:
> 
> Using inner functions its just as clean IMO.
> 
> void withRounding( int mode, void delegate() dg )
> {
>   setRoundingMode( mode);
>   dg();
>   restoreRoundingMode();
> }
> 
> Then to use it:
> 
> void foo()
> {
>   withRounding( ROUNDTOZERO, { ..do some stuff here } );
> }
> 
> -David

T With_ROUNDTOZERO(T)( lazy T dg )
{
	RoundingMode oldmode = setRoundingMode(ROUNDTOZERO);
	T ret = dg();
  	setRoundingMode(oldmode);
	return ret;
}

void foo()
{
	real r = With_ROUNDTOZERO(1+76);
}


all of that should inline (I assume*) to

void foo()
{
	RoundingMode oldmode = setRoundingMode(ROUNDTOZERO);
	real r = (1+76);
  	setRoundingMode(oldmode);
}


* does DMD inline lazy arguments when it inlines the function? For that 
matter does this get inlined?

void foo()
{
	int i;
	(){
		i=1;
	}();
}



More information about the Digitalmars-d-learn mailing list