www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1880] New: templates instantiated with non-constants should fail sooner

reply d-bugmail puremagic.com writes:

           Summary: templates instantiated with non-constants should fail
           Product: D
           Version: 1.027
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: wbaxter 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.

Feb 28 2008
parent d-bugmail puremagic.com writes:

------- Comment #1 from wbaxter gmail.com  2008-02-28 22:08 -------
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.

Feb 28 2008