1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

D言語で、ある型がclassかどうか判別する

Posted at

使う場面はあるはずなのにstd.traitsなどには見つからず、次のようなコードを考えていたのですが、

// 基底クラスを取得することができ、かつ基底クラスの中にObjectが含まれていればclass
template isClass(T)
{
    import std.traits : BaseTypeTuple;
    import std.meta : staticIndexOf;

    static if (__traits(compiles, BaseTypeTuple!T))
    	enum isClass = staticIndexOf!(Object, BaseTypeTuple!T) != -1;
    else
    	enum isClass = false;
}

これでOKです。

is(C == class)

classの部分を変えることで、structやinterfaceであるかどうかも調べることができます。

参考: D言語公式ドキュメント

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?