Thread overview
[Issue 2849] New: Length of a local immutable(char[]) cannot be a template value argument
Apr 17, 2009
d-bugmail
Mar 16, 2012
Don
Dec 25, 2012
Andrej Mitrovic
Jan 12, 2013
Andrej Mitrovic
April 17, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2849

           Summary: Length of a local immutable(char[]) cannot be a
                    template value argument
           Product: D
           Version: 2.027
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: clugdbug@yahoo.com.au


struct bar(int n){}
void main(){
immutable(char[]) x = "bug";
bar!(x.length) z;
}
-----
fog.d(3): Error: expression (x = "bug").length is not a valid template value
arg
ument
fog.d(4): Error: template instance fog.bar!((x = "bug").length) error
instantiat
ing
==========
Yet here are two very similar cases which work:

struct bar(int n){}
immutable(char[]) x1 = "bug";
void main(){
 immutable(char[]) x2 = "bug";
 enum q= x2.length;
 bar!(x1.length) z; // ok, it's a global
 bar!(q) z2; //ok, it's been copied to an enum
}


Probably related to issue #2841 and issue #2414.


-- 

March 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=2849



--- Comment #1 from Don <clugdbug@yahoo.com.au> 2012-03-15 22:40:16 PDT ---
It's because it doesn't try to const fold it. In this case,
the init value is constant, so it could possibly work. In other cases,
it could be initialized from something non-constant (eg, a function
parameter). Regardless, it should say that it's not constant, rather
than just that it's not a valid template parameter.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=2849


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-25 08:06:48 PST ---
(In reply to comment #1)
> It's because it doesn't try to const fold it. In this case,
> the init value is constant, so it could possibly work. In other cases,
> it could be initialized from something non-constant (eg, a function
> parameter). Regardless, it should say that it's not constant, rather
> than just that it's not a valid template parameter.

That first sample now works, but w.r.t. errors I can't reproduce the bad error message. E.g.:

struct bar (int n){}
void main()
{
    char[] x = "bug".dup;
    bar!(x.length) z;
}

test.d(5): Error: variable x cannot be read at compile time
test.d(5): Error: expression x.length is not a valid template value argument
test.d(5): Error: template instance test.bar!(x.length) error instantiating

Fixed?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 12, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=2849


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

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


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------