[Issue 1880] New: templates instantiated with non-constants should fail sooner
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Feb 28 19:58:44 PST 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1880
Summary: templates instantiated with non-constants should fail
sooner
Product: D
Version: 1.027
Platform: PC
OS/Version: Windows
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: wbaxter at gmail.com
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.
--
More information about the Digitalmars-d-bugs
mailing list