CTFE calling a template: Error: expression ... is not a valid template value argument

Jonathan M Davis jmdavisProg at gmx.com
Thu Sep 20 14:50:28 PDT 2012


On Thursday, September 20, 2012 23:22:36 Jens Mueller wrote:
> Hi,
> 
> I do not understand the following error message given the code:
> 
> string foo(string f)
> {
> if (f == "somestring")
> {
> return "got somestring";
> }
> return bar!(foo("somestring"));
> }
> 
> template bar(string s)
> {
> enum bar = s;
> }
> 
> I'll with dmd v2.060 get:
> test.d(7): called from here: foo("somestring")
> test.d(7): called from here: foo("somestring")
> test.d(7): called from here: foo("somestring")
> test.d(7): Error: expression foo("somestring") is not a valid template value
> argument test.d(12): called from here: foo("somestring")
> test.d(12): called from here: foo("somestring")
> test.d(7): Error: template instance test.bar!(foo("somestring")) error
> instantiating
> 
> In line 7 I call the template bar. But I call with the string that is
> returned by the CTFE of foo("somestring") which should return "got
> somestring" but instead it seems that an expression is passed. How do I
> force the evaluation foo("somestring")?
> I haven't found a bug on this.

Template arguments must be known at compile time. And even if you use foo at 
compile time, it has to be compiled before you use it, so you can't call it 
inside itself and pass that as a template argument. foo must be fully compiled 
before it can be called, and as it stands, it can't be fully compiled until 
it's called. So... Yeah. Not going to work.

- Jonathan M Davis


More information about the Digitalmars-d mailing list