Bug 196 – Static assertion involving typedef's base type fails strangely

Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2006-06-15T04:42:00Z
Last change time
2014-02-15T13:21:44Z
Keywords
diagnostic
Assigned to
bugzilla
Creator
matti.niemenmaa+dbugzilla

Comments

Comment #0 by matti.niemenmaa+dbugzilla — 2006-06-15T04:42:36Z
I need to static assert that a typedef's base type is equal to int. I tried the code below, but it fails with the message "static assert (is(int == int)) is false". If this is not meant to work, this is a bug in the sense that the error message makes no sense; if this is meant to work, it's a bug in the sense that it doesn't work. In the former case, feel free to change the priority, severity, and keywords to match: I'm currently assuming that this should work. -- typedef int x; static assert (is (typeof(typeid(x).base) == int)); -- Also, there's an extra space between "assert" and "(is" in the error message.
Comment #1 by thomas-dloop — 2007-01-01T04:25:40Z
The error message is incorrect, the assert should fail regardless. # # static assert (is (typeof(typeid(x).base) == int)); # x -> (typedef int) typeid(x) -> TypeInfo_Typedef(x) typeid(x).base -> TypeInfo(int) typeof(typeid(x).base) -> TypeInfo Thus rewriting the above assert: # # static assert(is(TypeInfo == int)); # To ensuring that a typedef's direct base type is an int: # # typedef byte X; # # static if(is(X == typedef) && is(X base == typedef)){ # static assert(is(typeof(base) == int)); # } #
Comment #2 by bugzilla — 2008-06-23T16:36:54Z
Thomas' code is almost correct. The correct code would be: typedef int X; static if (is(X base == typedef)) { static assert(is(base == int), "base of typedef X is not int"); } else { static assert(0, "X is not a typedef"); }