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

Jens Mueller jens.k.mueller at gmx.de
Thu Sep 20 15:11:51 PDT 2012


Jonathan M Davis wrote:
> 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.

I thought foo is interpreted at compile time.
There seems to be a subtle difference I'm not getting.
Because you can do the factorial using CTFE even though you have
recursion. I.e. there you have a call to the function itself. I.e. it
can be compiled because you just insert a call to the function. But for
a template you cannot issue something like call for instantiation.
Have to think more about it. But your answer helps a lot. Pushes me in
the right direction.

Jens


More information about the Digitalmars-d mailing list