On Thursday, 13 February 2020 at 08:06:52 UTC, Paul Backus wrote:
On Thursday, 13 February 2020 at 07:06:49 UTC, Jeff wrote:
Hello,

Was wondering if there was a simple, efficient way to unpack a variadic template argument. It needs to be efficient at runtime, and hopefully not use too much excessive CTFE.

C++ has the "..." operator, is there something equivalent in D?

    template<class ...Args>
    void g(Args... args) {
f(foo(args)...); // f(foo(args[0]), foo(args[1])); // etc
    }

What would be a good way to write that in D, with it being as efficient (no copies or building structs etc) and not use too much CTFE. Needing to use `.map` or similar at CTFE would be an example of too much CTFE.

    void g(Args...)(auto ref Args args) {
         // ?
    }

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.

Reply via email to