Bug 1880 – templates instantiated with non-constants should fail sooner

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2008-02-28T21:58:00Z
Last change time
2014-02-24T15:33:22Z
Keywords
wrong-code
Assigned to
nobody
Creator
wbaxter

Comments

Comment #0 by wbaxter — 2008-02-28T21:58:41Z
Or perhaps it should be called "static if(is()) allows a bogus type to pass". Here's the test case: enum Property {First=1,Second=2} struct CompileTimeCheck(Property Prop) { alias Prop prop; } struct CompileTime(Property Prop) { alias Prop prop; } struct RunTime { Property prop = Property.First; } void main() { alias CompileTime!(Property.First) CT; alias RunTime RT; static if (is(CompileTimeCheck!(CT.prop))) { pragma(msg, "CT ok"); } static if (is(CompileTimeCheck!(RT.prop))) { pragma(msg, "RT ok"); } alias CompileTimeCheck!(CT.prop) OK; // works no prob const z = OK.prop; alias CompileTimeCheck!(RT.prop) Fail; // fails if next line uncommentd //const y = Fail.prop; } This prints out both "CT ok" and "RT ok". It seems wrong to me both that RT passes the static if check and that the alias succeeds when using run-time value as a template parameter. It does fail if you try to actually access that .prop property, but it should fail sooner so that one can use static if's to determine if something is a compile-time accessible quantity or not. So far I haven't been able to figure out a way to do that, which is the ultimate objective. I have multiple structs, in some .prop is compile-time info in others it's run-time. I want to have a static if that handles the two cases differently but so far I can't figure out any way to write a working "isCompileTime!(T.prop)" template.
Comment #1 by wbaxter — 2008-02-28T22:08:36Z
You can also see it just by doing: Property x; static if(is(CompileTimeCheck!(x))) { pragma(msg, "CTC!(x) passed!"); } this shouldn't pass the is() check in my opinion. It's shouldn't be semantically valid to pass a non-constant runtime value to a template that requires a compile-time constant value.
Comment #2 by clugdbug — 2011-07-06T08:05:37Z