Constrained Templates
BCS
none at anon.com
Sun Jun 13 17:48:06 PDT 2010
Hello Ali,
> The method that I learned from Phobos is not the best, but at least
> the function interface reads better:
>
> T gcd(T)(T a, T b)
> if (supports_modulus!T)
> {
> T result;
> // ...
> return result;
> }
> That makes it clear that T must support the modulus operation.
>
> Here is how supports_modulus may be defined:
>
> template supports_modulus(T)
> {
> const bool supports_modulus = is (typeof(
> {
> T a;
> T b;
> T c = a % b;
> }()));
> }
this shorter vertion also works:
template supports_modulus(T)
{
const bool supports_modulus = is (typeof(T.init%T.init) == T);
}
--
... <IXOYE><
More information about the Digitalmars-d
mailing list