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

Picaud Vincent via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 7 13:37:50 PST 2016


On Monday, 7 November 2016 at 21:23:37 UTC, Picaud Vincent wrote:
> On Monday, 7 November 2016 at 18:59:24 UTC, Jerry wrote:
>> 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;
>
> Hi Jerry,
>
> Thank you so much for your quick answer! I tried your 
> suggestion and it works.
>
> My main interest is numerical computations. I have some C++ 
> libs using meta-programming and I want to see how I can 
> translate some parts in D. The goal is to check: productivity & 
> code readability & performance. I will try to implement 2 toy 
> examples:
>
> 1/ A basic example of strided dense vector structure dealing 
> with the dynamic/static size in an uniform way. In D I thing 
> this can be done with something like this (not tried yet to 
> compile it, but that is the idea to mimick my C++ 
> implementation)
>
> struct Vector(T,SIZE,STRIDE) if( 
> (is(SIZE==size_t)||isIntegralConstant!SIZE) ...)
> {
>   alias T ElementType;
>
>   private SIZE size_;
>   private STRIDE stride_;
>
>   ...
>
>   auto required_capacity() { return size_*stride_; } // return 
> a size_t or a IntegralConst
>
>   static if ( isIntegralConstant!(typeof(required_capacity()) )
> {
> }
> else
> {
> }
>
> }

Premature post send by error sorry.... Well something like:

    static if ( isIntegralConstant!(typeof(required_capacity()) )
      ElementType[required_capacity()] data_;
    else
      ElementType[] data_;
}

For that, at least in C++, I need integral_constant<> type with 
compile-time arithmetic and smooth integration with "usual" 
size_t/ptrdiff_t types.

2/ I also would like to test some implementations concerning 
automatic differentiation.
I have my own C++ libs, inspired, but ~20% faster than Adept:
http://www.met.reading.ac.uk/clouds/adept/
and I would like to know how I can do that in D

Well... That is the idea... I hope I will get some results and I 
will be happy to share if it is something interesting.

Vincent



More information about the Digitalmars-d-learn mailing list