简体   繁体   中英

Referring to template parameter pack in class

Since C++17 we got optional template parameter packs. But how do I refer to that in my class? Does anyone have a good example for it? Thanks!

https://gcc.godbolt.org/z/485Z0J

template<auto...>
struct C { };

int main()
{
    C<'C', 0, 2L, nullptr> x;
    return 0;
}

https://en.cppreference.com/w/cpp/language/template_parameters

First of all a little bit of terminology. That's not an "optional template parameter pack". That is a variadic non-type template arguments with auto.

There are several ways to refer to them, but you need to give the variadic a name. Here are some examples:

#include <tuple>

template <class... Args>
auto foo(Args...) -> void;

template<auto... Args>
struct C
{
    static constexpr std::tuple<decltype(Args)...> t{Args...};

    auto call_foo()
    {
        foo(Args...);
    }
};

auto test()
{
    C<'C', 0, 2L, nullptr> x;
    x.call_foo();

    return std::get<2>(x.t);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM