static assert

rm roel.mathys at gmail.com
Sat Oct 21 03:22:18 PDT 2006


thx,

so that the code beneath works (with the mixin),
is (bad) luck?

roel
// -----------------------------------
template Factor(int n:0)
{
	const int Factor = 1;
}

template Factor(int n)
{
	pragma(msg, itoa!(n));
//	static assert(n>0);
	mixin AssertWithMsg!(n>0,"only positive integers allowed");
	const int Factor = n * Factor!(n-1);
}

template AssertWithMsg(bool cond:false, char[] s)
{
	pragma(msg,s);
	static assert(false);
}

template AssertWithMsg(bool cond:true, char[] s)
{
}

import std.stdio;
void main()
{
	writefln(Factor!(-5));
}



More information about the Digitalmars-d-learn mailing list