www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2379] New: CTFE fails to evaluate if unless it is very straightforward

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2379

           Summary: CTFE fails to evaluate if unless it is very
                    straightforward
           Product: D
           Version: 2.019
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: 2korden gmail.com


First attempt:
-----------
template Test0(invariant(char)[] text)
{
    auto Test0 = text;
}

writefln(Test0!("hello")); // success
pragma(msg, Test0!("hello")); // failure:
// pragma msg string expected for message, not 'Test0'

Let's replace invariant(char)[] with alias :(
-----------
template Test0(alias text)
{
    auto Test0 = text;
}
pragma(msg, Test0!("hello")); // success

Let's make it slightly more complex:
-----------
template Test0(alias text)
{
    auto Test0 = text[0..1];
}
pragma(msg, Test0!("hello")); // failure:
// pragma msg string expected for message, not 'Test0'

Other example:
-----------
template Test0(invariant(char)[] text)
{
    auto Test0 = text;
}

template Test1(invariant(char)[] text)
{
    auto Test1 = Test0!(text);
}

writefln(Test0!("hello")); // success
writefln(Test1!("hello")); // failure:
// Error: non-constant expression Test0


-- 
Sep 29 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2379







Something is wrong here. The examples don't use CTFE. Is the title wrong?


-- 
Sep 29 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2379






Everything works as expected.

template Test0(invariant(char)[] text)
{
    auto Test0 = text;
}

effectively declares

invariant(char)[] Test0 = "hello";

which is a variable initialized at runtime. The correct way
to write this template is

template Test0(invariant(char)[] text)
{
    enum Test0 = text;
}

which declares a manifest constant.


-- 
Sep 30 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2379


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID





Sergey is correct.


-- 
Oct 02 2008