确定模板参数是否是包含相同非限定类型的数组

determine whether the template arguments are arrays holding the same unqualified type

我正在尝试制作一个模板来确定参数是否是包含相同非限定类型的数组。

template sameUnqualArrays(A, B) {
  enum bool sameUnqualArrays = isArray!A && isArray!B &&
      is(Unqual!(ForeachType!A) == Unqual!(ForeachType!B));
}

unittest {
  static assert(sameUnqualArrays!(ubyte[], immutable(ubyte[])));
  static assert(!sameUnqualArrays!(ubyte[], immutable(byte[])));
  static assert(!sameUnqualArrays!(ubyte[], ubyte));
}

不幸的是,最后一个断言没有通过但给出了错误: /usr/include/dmd/phobos/std/traits.d(6062,9): Error: invalid foreach aggregate cast(ubyte)0u

这是一个错误吗?如何解决这个问题?有没有更简单的方法来实现这个?

嗯,短路评估似乎在这里不起作用?这可能是一个错误,但解决方法很简单:

import std.traits;

template sameUnqualArrays(A, B) {
  static if (isArray!A && isArray!B)
    enum bool sameUnqualArrays =
      is(Unqual!(ForeachType!A) == Unqual!(ForeachType!B));
  else
    enum bool sameUnqualArrays = false;
}

unittest {
  static assert(sameUnqualArrays!(ubyte[], immutable(ubyte[])));
  static assert(!sameUnqualArrays!(ubyte[], immutable(byte[])));
  static assert(!sameUnqualArrays!(ubyte[], ubyte));
}