www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templates with strings

reply Mike Linford <mike.linford.reg gmail.com> writes:
I'm trying to compile the following file. Can somebody explain to me how 
I can rewrite it so that I don't get "Error: non-constant expression"? 
All I'm trying to do is create, using templates, a string of all 
characters between two given characters:

  1 module ua;
  2 
  3 template dcharInterval(immutable(dchar) start, immutable(dchar) stop)
  4 {
  5    static assert(stop >= start);
  6    static if(start == stop)
  7       dstring dcharInterval = [start];
  8    else
  9       dstring dcharInterval = [start] ~ dcharInterval!(start + 1, 
stop);
 10 }
 11 
 12 dstring LATIN = "\u00AA"d ~ "\u00BA"d ~ dcharInterval!(0x00C0, 
0x00D6);
 13 

Thanks.

-- 
Mike Linford
Jul 03 2010
next sibling parent reply BCS <none anon.com> writes:
Hello Mike,

 I'm trying to compile the following file. Can somebody explain to me
 how I can rewrite it so that I don't get "Error: non-constant
 expression"? All I'm trying to do is create, using templates, a string
 of all characters between two given characters:
 

1) where did you get that error? (I'm lazy, sorry) 2) is there a reason you don't use CTFE? dchar[] Between(dchar start, dchar stop) { if(start > stop) { auto t = start; start = stop; stop = t; } dchar[] ret; for(dchar d = start; d<=stop; d++) ret ~= d; return ret; } import std.stdio; void Go(dchar[] s)(){writef("%s\n", s); } void main(){Go!(Between('a','g'))();}
 1 module ua;
 2
 3 template dcharInterval(immutable(dchar) start, immutable(dchar)
 stop)
 4 {
 5    static assert(stop >= start);
 6    static if(start == stop)
 7       dstring dcharInterval = [start];
 8    else
 9       dstring dcharInterval = [start] ~ dcharInterval!(start + 1,
 stop);
 10 }
 11
 12 dstring LATIN = "\u00AA"d ~ "\u00BA"d ~ dcharInterval!(0x00C0,
 0x00D6);
 13
 Thanks.
 

... <IXOYE><
Jul 03 2010
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Mike Linford wrote:

 2. The reason I don't use CTFE is because I don't know how to be certain
 its been called at compile time. Apparently using a result in a template
 like you did will accomplish that, but is there a way I can be sure
 without making up bogus empty templates?

There is relatively detailed information on what types of functions can be called at compile time: http://www.digitalmars.com/d/2.0/function.html#interpretation <quote> In order to be executed at compile time, the function must appear in a context where it must be so executed, for example: * initialization of a static variable * dimension of a static array * argument for a template value parameter </quote> Ali
Jul 04 2010
prev sibling next sibling parent Mike Linford <mike.linford.reg gmail.com> writes:
On Sun, 04 Jul 2010 00:43:50 +0000, BCS wrote:

 Hello Mike,
 
 I'm trying to compile the following file. Can somebody explain to me
 how I can rewrite it so that I don't get "Error: non-constant
 expression"? All I'm trying to do is create, using templates, a string
 of all characters between two given characters:
 
 

you don't use CTFE? dchar[] Between(dchar start, dchar stop) { if(start > stop) { auto t = start; start = stop; stop = t; } dchar[] ret; for(dchar d = start; d<=stop; d++) ret ~= d; return ret; } import std.stdio; void Go(dchar[] s)(){writef("%s\n", s); } void main(){Go!(Between('a','g'))();}
 1 module ua;
 2
 3 template dcharInterval(immutable(dchar) start, immutable(dchar) stop)
 4 {
 5    static assert(stop >= start);
 6    static if(start == stop)
 7       dstring dcharInterval = [start]; 8    else
 9       dstring dcharInterval = [start] ~ dcharInterval!(start + 1,
 stop);
 10 }
 11
 12 dstring LATIN = "\u00AA"d ~ "\u00BA"d ~ dcharInterval!(0x00C0,
 0x00D6);
 13
 Thanks.


Hi BCS, 1. I was getting that error on lines 9 and 12. I got it a couple dozen times (I assume once for each template instance). 2. The reason I don't use CTFE is because I don't know how to be certain its been called at compile time. Apparently using a result in a template like you did will accomplish that, but is there a way I can be sure without making up bogus empty templates? Thanks. -- Mike Linford
Jul 03 2010
prev sibling next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Mike Linford <mike.linford.reg gmail.com> wrote:

 2. The reason I don't use CTFE is because I don't know how to be certain
 its been called at compile time. Apparently using a result in a template
 like you did will accomplish that, but is there a way I can be sure
 without making up bogus empty templates?

Anything that is static, that is, static member variables, globals with initializers, and enums, will be CTFE'd. An enum is probably the best match for you. -- Simen
Jul 03 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--0016e6dd8c74594b99048a8ce1c2
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Sun, Jul 4, 2010 at 09:34, Ali =C7ehreli <acehreli yahoo.com> wrote:

 Mike Linford wrote:

 2. The reason I don't use CTFE is because I don't know how to be certai=


 its been called at compile time. Apparently using a result in a templat=


 like you did will accomplish that, but is there a way I can be sure
 without making up bogus empty templates?

There is relatively detailed information on what types of functions can b=

 called at compile time:

  http://www.digitalmars.com/d/2.0/function.html#interpretation

 <quote>
 In order to be executed at compile time, the function must appear in a
 context where it must be so executed, for example:

    * initialization of a static variable
    * dimension of a static array
    * argument for a template value parameter
 </quote>

And, traditionally, being defined as an enum (a C/C++ inheritance?) enum thingy =3D funToBeTested(); If it compiles, then funToBeTested works at compile-time. Is there not a __ctfe trait or somesuch? Philippe --0016e6dd8c74594b99048a8ce1c2 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Sun, Jul 4, 2010 at 09:34, Ali =C7ehreli <spa= n dir=3D"ltr">&lt;<a href=3D"mailto:acehreli yahoo.com" target=3D"_blank">a= cehreli yahoo.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote= " style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, = 204); padding-left: 1ex;"> <div>Mike Linford wrote:<br> <br> &gt; 2. The reason I don&#39;t use CTFE is because I don&#39;t know how to = be certain<br> &gt; its been called at compile time. Apparently using a result in a templa= te<br> &gt; like you did will accomplish that, but is there a way I can be sure<br=

<br></div> There is relatively detailed information on what types of functions can be = called at compile time:<br> <br> =A0<a href=3D"http://www.digitalmars.com/d/2.0/function.html#interpretatio= n" target=3D"_blank">http://www.digitalmars.com/d/2.0/function.html#interpr= etation</a><br> <br> &lt;quote&gt;<br> In order to be executed at compile time, the function must appear in a cont= ext where it must be so executed, for example:<br> <br> =A0 =A0* initialization of a static variable<br> =A0 =A0* dimension of a static array<br> =A0 =A0* argument for a template value parameter<br> &lt;/quote&gt;<font color=3D"#888888"><br></font></blockquote><div><br><br>= And, traditionally, being defined as an enum (a C/C++ inheritance?)<br><br>= enum thingy =3D funToBeTested();<br><br>If it compiles, then funToBeTested = works at compile-time.<br> <br>Is there not a __ctfe trait or somesuch?<br><br>Philippe<br></div></div=

--0016e6dd8c74594b99048a8ce1c2--
Jul 04 2010