Can't use variadic arguments to functions that use templates
JS
js.mdnq at gmail.com
Tue Jul 23 07:02:58 PDT 2013
On Monday, 22 July 2013 at 16:48:56 UTC, Jesse Phillips wrote:
> On Sunday, 21 July 2013 at 07:22:08 UTC, JS wrote:
>> void foo(T...)(T t)
>> {
>> pragma(msg, B(t));
>> }
>>
>> void main() {
>> foo("x", "a", "b");
>> din.getc();
>> }
>>
>>
>> does work. I need to have B generate compile time code so it
>> is efficient. Your method calls an actual function at runtime
>> so it is nearly as fast as it can be.
>
> My method doesn't make any calls, your foo is a called at
> runtime. There is no way to observe a function at compile time;
> ctfeWriteln does not exist.
>
> Also don't stringof variable v (I assume you want the value and
> not the symbol concatenated.
>
> template B(T...) {
> string B(T b) {
> string s;
> foreach(Type; T) pragma(msg, Type.stringof);
> foreach(v; b) s ~= v;
> return s;
> }
> }
>
> string foo(T...)(T t)
> {
> return B(t);
> }
>
> void main() {
> enum forced = foo("x", "a", "b");
> pragma(msg, forced);
> din.getc();
> }
I don't think you understand(or I've already got confused)...
I'm trying to use B has a mixin(I don't think I made this clear).
I can't use it as a normal function. e.g., I can't seem to do
mixin(B(t)). If I could, this would definitely solve my problem.
Here is a real world example:
template VariadicLoop(T...)
{
pragma(msg, T);
string VariadicLoop(T t)
{
string s;
foreach(k; T)
s ~= k.stringof;
foreach(v; t)
s ~= v;
return s;
}
//enum tVariadicLoop = eval();
//pragma(msg, tVariadicLoop);
}
string join(T...)(string delim, T t)
{
return mixin(VariadicLoop(t));
}
VariadicLoop should join the strings passed to join(producing
whatever code is necessary to do this efficiently.
e.g.,
join(",", "a", s) should result in the code for join:
{
return "a"~","~s; // s maybe t[1] or something
}
This is the most efficient way to join rather than using a
foreach over an array of strings or something or another.
(VariadicLoop could handle string[]'s and insert the appropriate
code to join them)
The goal I'm trying to solve is efficiently deal with variadic
arguments that are compile time calculable along with mixing in
the runtime aspect. Basically: get the compiler to do all the
work it can before having it done at runtime. if we are passing
string literals to join then they can be joined at compile time.
If we are passing a combination of string literals, string
variables, and string arrays then we deal with each part
efficiently as possible.
More information about the Digitalmars-d-learn
mailing list