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
{
}

}

Reply via email to