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 14:18:56 PST 2016


On Monday, 7 November 2016 at 21:37:50 UTC, Picaud Vincent wrote:
>>   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

Ah I get what you mean, you can do that without using a special 
type.

     struct Vector(T, Args...) if(Args.length == 1)
     {
         static if(is(Args[0] == size_t))
         {
             size_t size;
         }
         else static if(Args[0] != 0) // would error if it's a 
type that's not size_t
         {
             enum size = Args[0];
         }
         else
         {
             static assert(0);
         }
     }

     Vector!(int, 10) a;
     Vector!(int, size_t) b; // both work with IntegralConstant

could use __traits(compiles) to see if it's not a type, for that 
second static if. Which would probably be better, so if you pass 
a float or something, it won't give a weird error.


More information about the Digitalmars-d-learn mailing list