Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell whether the template argument is a struct

Tags:

traits

d

How can I make a template that tells whether the argument is a struct or not? I.e. how to make the following code run without an error:

struct X { int a; }
static assert(isStruct!X);
static assert(!isStruct!int);
like image 596
Tamas Avatar asked Feb 10 '23 08:02

Tamas


1 Answers

Use is expression.

struct X { int a; }
static assert(is(X == struct));
static assert(!is(int == struct));

But if you really want a template:

template isStruct(T)
{
    enum bool isStruct = is(T == struct);
}
like image 64
sigod Avatar answered Feb 19 '23 11:02

sigod