Template Constraints
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sat Feb 24 03:43:25 UTC 2018
On Saturday, February 24, 2018 03:30:45 psychoticRabbit via Digitalmars-d-
learn wrote:
> On Saturday, 24 February 2018 at 02:54:13 UTC, Jonathan wrote:
> > I am having trouble finding many useful explanations of using
> > template constraints beyond basic usage.
> >
> > I would like to have a template constrant to enforce that a
> >
> > type can be explicitly cast to another type:
> > void (T)(T t)
> >
> > if (cast(int) T)//force `cast(int) T` to be possible
> >
> > {
> >
> > // Yay I know `t` can be cast to an `int`!
> >
> > }
> >
> > Is this possible?
>
> import std.traits : isIntegral;
> void testTemplate(T)(T x) if (isIntegral!T)
> {
> writeln(x, " is an integral. yeah!");
>
> }
That does not do what the OP requested at all. That tests whether T is one
of byte, ubyte, short, ushort, int, uint, long, and ulong, whereas what the
OP wants is to test whether T can be cast to int.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list