digitalmars.D.learn - Dealing with variadic template parameters
- JS (52/52) Jul 20 2013 I am trying to write a simple template expansion of boolean
I am trying to write a simple template expansion of boolean relations between values. I've tried various ways but this is the only way I can get to work without a compile time error: template A(string, T, R...) { string A(string op, string s, T t, R r) { string n = "("~s~" == "~t~")"; static if (r.length) return n~" "~op~" "~A(op, s, r); return n; } } But I do not think this method is ctfe as I can't use it as a mixin or else the arguments can't be read at compile time. the idea is simple A!("||", "a", d) should expand out to the code(as a string at compile time) (a == d[0]) || (a == d[1]) || ... so I can do stuff like void foo(T)(string a, T d...) { if (a == "a" && !(mixin(A!("||", "a", d)))) ... When I try to use pure ctfe I can't pass the variadic without template matching issues. I initially tried something like: template A(string op, string s, string c, R...) { string eval(string c) { return "AAA"; } string eval(string t, R r) { string n = "("~s~" == "~t~")"; static if (r.length) return n~" "~op~" "~eval(op, s, r); return n; } enum A = eval(c); } I've tried template A(alias S) { string eval(T t...) { } enum A = eval(S); } but it can't be used with multiple parameters(we sort of need a variadic alias to make it work). In any case, hopefully someone can come up with an efficient template that expands the (varadic) arguments at compile time(no overhead, same as if I typed the code in by hand). I'm trying to use such a function in split(string s, T delims) where T can be a string or char). so I can do split(s, ",", '.', " ") or split(s, " ") (accepts variable number of arguments) efficiently as possible. (no wasted overhead for multiple arguments more than necessary(such as calling a recursive function, etc...).
Jul 20 2013