September 03, 2014 Template instantiation and string vs const char[] | ||||
---|---|---|---|---|
| ||||
I have this piece of code: template somet(R...) { static if (R.length) { string somet = "[\n"~R[0]~"\n" ~ somet!(R[1..R.length]) ~ "]\n"; } else string somet = ""; } void main() { writeln(somet!("name","tame")); writeln(somet!"name"); } When I compile it, it generates the error message "static variable somet cannot be read at compile time". When I replace 'string' with 'const char[]', it works. Can anyone explain why this happens? Thanks. Regards |
September 03, 2014 Re: Template instantiation and string vs const char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jason den Dulk | On Wednesday, 3 September 2014 at 05:18:42 UTC, Jason den Dulk wrote: [...] While not really answering your question, I believe the idiomatic way is to use enum here instead of string/char[]. Conjecture: strings are final but the symbol can be redirected to point to new arrays, such as results of concatenations. Concatenations create new and unique arrays upon each operation as each by definition cannot be altered. char[] can be *appended* to, technically retaining the same array and merely reallocating (potentially moving) to fit the extra content. This sounds like something the compiler should be able to work around but apparently it doesn't. Also, note that your check of (R.length) will only evaluate to false if the template is instantiated as somet!(). |
Copyright © 1999-2021 by the D Language Foundation