New to D and mimicking C++ : how to implement std::integral_constant<>?

Jerry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 7 10:59:24 PST 2016


On Monday, 7 November 2016 at 18:42:37 UTC, Picaud Vincent wrote:
> template isIntegralConstant(ANY)
> {
>     enum bool 
> isIntegralConstant=__traits(identifier,ANY)=="IntegralConstant";
> }

A bit more elegant way of doing that would be:

enum isIntegralConstant(T) = is(T : IntegralConstant!U, U...);


> I would be very graceful for any help/advice that explains the 
> right way to implement C++ std::integral_constant<T,T value> in 
> the D language.
>
> Vincent

Now the question is, do you really need IntegralConstant? I've 
never used it in C++ so I don't really know any of the use cases 
for it. But generally in D if you need something to be a compile 
time constant value you can just use "enum". It can be any type 
as well, so long as it can be evaluated at compile time.

enum long someConstant = 1 << 32;




More information about the Digitalmars-d-learn mailing list