Unable to instantiate template with same name as function

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 2 23:55:54 PST 2016


On Thursday, March 03, 2016 11:42:13 Shriramana Sharma via Digitalmars-d-learn 
wrote:
> Hello. I have a function I want to make CTFE-able as a template.
>
> string ta(string s) { return s ~ "1"; }
> template ta(string s) { enum ta = ta(s); }
> void main() { string s = ta!"s"; }
>
> Compiling the above I get the errors:
>
> <src>(2): Error: forward reference of variable ta
> <src>(3): Error: template instance <src>.ta!"s" error instantiating
>
> Please clarify what I am doing wrong? Thanks!

You can't overload a function and an eponymous template like that. They need
to have distinct names. Now, that being said, I don't understand why you'd
need to do anything with a template for CTFE in this case. Just write the
function and then call it in a context that requires a compile-time result,
and it should work. e.g.

void main()
{
    enum s = ta("s");
}

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list