generic + numeric + literals = abomination

#ponce nospam at nospam.com
Sun Mar 28 08:09:18 PDT 2010


so Wrote:

> Basically what i am asking is hmmm, ability to write generic constants? :)
> 
> Thanks!

Hi,

When writing generic FP code i always use real literals and cast to T, or int. I suggest doing this.
Like:

T exp3(T)(T x)
{
	if (x < cast(T)(-1.15365L))
	{
		return 0;
	}
	else
	{
		return cast(T)1.L + x * (cast(T)1.0L + x * (cast(T)0.5L + x * cast(T)0.3333333333333L));
	}
}


I assume the compiler will be able to optimize the cast in release mode.

If you really wan't to prevent this cast in debug mode, you can simply go with:

const T myConst = 0.3L;

then the cast will be compile-time. 



More information about the Digitalmars-d-learn mailing list