d检查类型是指针

原文

void main() {
  char accepted_type;
  char* non_accepted_type;

  if (__traits(isPointer, typeof(accepted_type))) {
    // 不接受类型
  }

 else { /*不是指针,接受*/ }

  if (__traits(isPointer, typeof(non_accepted_type))) {
    //不接受类型
  }

 else { /*不是指针,接受*/ }
}

可查看源代码,这里:

enum bool isPointer(T) = is(T == U*, U) && __traits(isScalar, T);

可用is()表达式:

if (is(typeof(accepted_type) == T*, T))
{//如果接受类型匹配T*,则T就是类型
    // 它是指针
}

亚当的is表达式

posted @ 2022-06-26 09:06  zjh6  阅读(12)  评论(0)    收藏  举报  来源