__traits(compiles, ...) with syntax errors

Steven Schveighoffer schveiguy at gmail.com
Mon Jan 14 21:47:40 UTC 2019


How many times have you written something like:

void foo(T)(T t) if (__traits(compiles, t.bar())) // or is(typeof(t.bar()))
{
    t.bar();
}

And somehow, somewhere, this isn't called? Then you remove the 
constraint, and find that there's a syntax error in the bar template 
function (like a missing semicolon).

I often times, to avoid this, simply just use __traits(hasMember, T, 
"bar"), and live with the consequences.

I'd love to have something like __traits(compiles), but have it be an 
error if the syntax is incorrect.

In other words, the constraint above only fails if there is a missing 
symbol, or incorrect type conversion, not that there is a stupid syntax 
error that would NEVER compile.

Ironically, it would be something like compilesOrSyntaxError.

Any thoughts?

-Steve


More information about the Digitalmars-d mailing list