specializing template with string variable in CTFE
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Oct 26 03:48:34 PDT 2014
Hello.
the following code is not working:
template prstr(string s) {
enum prstr = "write("~s.stringof~");\n";
}
string buildWriter() (string fmt) {
return prstr!(fmt[0..$-1]);
}
string writer(string fmt) () {
enum s = buildWriter(fmt);
return s;
}
void main () {
import std.stdio;
writeln(writer!"str"());
}
z40.d(6): Error: variable fmt cannot be read at compile time
z40.d(10): Error: template instance z40.buildWriter!() error instantiating
z40.d(16): instantiated from here: writer!"str"
but why? fmt is known in CTFE and compiler can use it as string literal
for instantiating `prstr`.
please, don't mind the idiocity of the code: this is just a sample to
show what confuses me. i know about possibility of moving `fmt` to
template argument, but i need it as function argument, 'cause
`buildWriter()` actually does string processing and i want to
instantiate `prstr` with the part of the string. and this processing
cannot be done in 'foreach'.
and writing everything in functional style sux: it leads to explosive
growing of the number of arguments passed to templates.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141026/fbfcd0f0/attachment.sig>
More information about the Digitalmars-d-learn
mailing list