is(some template instantiation) is true, but the actual instantiation fails

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 29 08:14:50 PST 2016


On Friday, 29 January 2016 at 15:28:29 UTC, Adrian Matoga wrote:
> How can I reliably test if CallsFoo can be instantiated?

You can use a constraint to prevent invalid instantiation:

struct HasFoo { void foo() {} }

struct NoFoo {}

struct CallsFoo(T)
if (__traits(hasMember, T, "foo"))
{
	T t;
	void bar() { t.foo(); }
}

static assert(is(CallsFoo!HasFoo));
static assert(!is(CallsFoo!NoFoo));





More information about the Digitalmars-d-learn mailing list