Unpack Variadic Args?
Steven Schveighoffer
schveiguy at gmail.com
Thu Feb 13 17:13:31 UTC 2020
On 2/13/20 11:29 AM, Jeff wrote:
> On Thursday, 13 February 2020 at 08:06:52 UTC, Paul Backus wrote:
>> Variadic template arguments unpack automatically in D, so you don't
>> need to do anything special here:
>>
>> void g(Args...)(auto ref Args args) {
>> import core.lifetime: forward; // like std::forward
>> f(forward!args);
>> }
>>
>> You can read more about variadic template arguments in this article:
>>
>> https://dlang.org/articles/ctarguments.html
>
> That would result in the call:
>
> f( args[0], args[1], ... );
>
> But the C++ version does the following:
>
> f( foo(args[0]), foo(args[1]), ... );
>
> They are different.
the f(foo(args)...) syntax doesn't have a D equivalent.
I don't think it's possible to do without some form of mixin. While
compile-time lists are available, they must be strictly made of things
that are either CTFE expressions or symbols.
A possible mixin solution:
string doMixin(T...)(string formatspec)
{
string result;
import std.format: format;
static foreach(i; 0 .. T.length)
result ~= format(formatspec, __traits(identifier, T[i])) ~ ",";
return result[0 .. $-1]; // trim last comma
}
void g(T...)(T args) {
mixin("f(" ~ doMixin!args("foo(%s)") ~ ");");
}
-Steve
More information about the Digitalmars-d-learn
mailing list