How to define a class that can store variable template arguments?

template<typename... Args>
struct A
{
    Args&... args_;
    //
    // error : only function and template parameters can be parameter packs
    //

    A(Args&... args) : args_(args)
    {}
};

int main()
{
    auto a = A(1, 2, 3);
}

My compiler clang 5.0with -std=c++1z.

How to define a class that can store variable template arguments in this case?

+4
source share
1 answer

This is not possible, as far as I know. You should use std::tupleto store the parameters:

template<typename... Args>
struct A
{
    std::tuple<std::decay_t<Args>...> args_;

    A(Args&&... args) : args_(std::make_tuple(std::forward<Args>(args)...))
    {}
};

With C ++ 17, you can use std::applyfor cal functions args_as parameters instead of unpacking them

std::apply(func, args_);
+6
source

Source: https://habr.com/ru/post/1673220/


All Articles