static check for float type or existence of a function overload

Thomas Kuehne thomas-dloop at kuehne.cn
Tue Oct 24 15:31:57 PDT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill Baxter schrieb am 2006-10-24:
> I'd like to do something like this:
>      static if (is(Type.epsilon)) {
>          // Type is fp type
>          alias Type RType;
>          static final bool isfloat = true;
>      } else {
>          // type is not fp type
>          alias real RType;
>          static final bool isfloat = false;
>      }
>
> I thought the is(Type.epsilon) would fail only if Type didn't have an 
> epsilon, but it seems to fail no matter what.

is(typeof(Type.epsilon))

> Actually what I *really* want to do is just special case types for which 
> a particular funtion is not defined.  E.g. sqrt().  If sqrt() doesn't 
> exist for the type then I'd like to do something else (because it won't 
> compile with the call to sqrt in there...)
>
> Is there a way to achieve that other than the enumeration of the things 
> I know have a sqrt?  Problem with that is a user can easily provide a 
> sqrt for their type.  So I have no way of knowing in advance what types 
> have sqrt functions and which don't.

sure:

static if(is(typeof(sqrt(Type.init)))){
	...
}

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFFPqD5LK5blCcjpWoRAg/zAJ0XO07miGQbmUdy3bAFx4F4Uvi7NwCeIR3S
a+FCYilH4V0gpsso/sXN6rc=
=Zj6R
-----END PGP SIGNATURE-----



More information about the Digitalmars-d-learn mailing list