Check type is a struct at compile time?

Kapps via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 5 12:45:26 PST 2014


On Friday, 5 December 2014 at 20:38:31 UTC, Gary Willoughby wrote:
> How can i check that a type is a struct at compile time? I know 
> i can test for a class like this:
>
> static if(is(T == class))
> {
>     ...
> }
>
> But how to do the same thing for a struct?

Same thing but with struct: is(T == struct)

Note that this returns false for things like 'int'.

Ex:

struct Foo { }

void foo(T)(T inst) {
	static if(is(T == struct))
		writeln(T.stringof, " is a struct");
	else
		writeln(T.stringof, " is NOT a struct");
}

void main() {
	Foo a;
	foo(a);
	foo(3);
}

--

Foo is a struct
int is NOT a struct


More information about the Digitalmars-d-learn mailing list