enum template shorthand and short circuit evaluation
Byron via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jun 9 17:28:46 PDT 2014
Should this work? It seems like the short circuit booleans are not
working:
--------------------------------
import std.traits;
enum isPrimitive(T) = isBasicType!T || (isArray!T && isBasicType!
(ForeachType!T));
void main() {
assert(isPrimitive!int);
assert(isPrimitive!char);
assert(isPrimitive!string);
assert(isPrimitive!(byte[]));
assert(!isPrimitive!test1);
assert(!isPrimitive!test2);
assert(!isPrimitive!test3);
}
class test1 {}
struct test2 {}
interface test3 {}
----------------------------------------
dmd test
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate 0
test.d(5): Error: template instance std.traits.ForeachType!int error
instantiating
test.d(9): instantiated from here: isPrimitive!int
test.d(9): Error: template instance test.isPrimitive!int error
instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate '\xff'
test.d(5): Error: template instance std.traits.ForeachType!char error
instantiating
test.d(10): instantiated from here: isPrimitive!char
test.d(10): Error: template instance test.isPrimitive!char error
instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate null
test.d(5): Error: template instance std.traits.ForeachType!(test1) error
instantiating
test.d(14): instantiated from here: isPrimitive!(test1)
test.d(14): Error: template instance test.isPrimitive!(test1) error
instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate test2()
test.d(5): Error: template instance std.traits.ForeachType!(test2) error
instantiating
test.d(15): instantiated from here: isPrimitive!(test2)
test.d(15): Error: template instance test.isPrimitive!(test2) error
instantiating
C:\D\dmd2\windows\bin\..\..\src\phobos\std\traits.d(5762): Error: invalid
foreach aggregate null
test.d(5): Error: template instance std.traits.ForeachType!(test3) error
instantiating
test.d(16): instantiated from here: isPrimitive!(test3)
test.d(16): Error: template instance test.isPrimitive!(test3) error
instantiating
--------------------------------
DMD32 D Compiler v2.065
But this style works:
template isPrimitive(T)
{
static if(isBasicType!T || (isArray!T && isBasicType!(ForeachType!T)))
{
enum isPrimitive = true;
} else {
enum isPrimitive = false;
}
}
-Byron
More information about the Digitalmars-d-learn
mailing list