throws or the like

Christopher Wright dhasenan at gmail.com
Thu Apr 24 07:27:38 PDT 2008


bearophile wrote:
> Later I have found that I use static asserts in my templates/functions, and I need to unittestes those too, so I'd probably enjoy a static version, able to catch static asserts etc:
> 
> static throws(foo(10), StaticException1);
> static throws(foo(10), StaticException1, StaticException2, ...);
> 
> So far I haven't found a way to test such static assertions/errors, if you have suggestions I'd love to hear them.

template template_throws(alias theTemplate, Args...)
{
	// If you have d2, you can use:
	// static if (__traits(compiles, theTemplate!(Args)))
	static if (is (typeof (theTemplate!(Args))))
	{
		enum template_throws = false;
	}
	else
	{
		enum template_throws = true;
	}
}

template Foo(int i)
{
	static assert (i > 1);
	const Foo = i;
}

static assert (template_throws!(Foo, 1));
static assert (!template_throws!(Foo, 2));

I don't see any static exceptions in D, so I can't handle them.

> Bye,
> bearophile



More information about the Digitalmars-d mailing list