Skip to content

C++:ParameterPack

Example

template <typename ... Args>
inline static Message makeMessage(Args && ... args) noexcept
{
    return Message(std::forward<Args>(args) ...);
}

Access Nth element

template<int N, typename... Ts> using NthTypeOf =
        typename std::tuple_element<N, std::tuple<Ts...>>::type;

Usage:

using ThirdType = NthTypeOf<2, Ts...>;

sizeof ... operator

sizeof ...연산자를 사용하면 파라미터의 개수를 알아낼 수 있다.

#include <iostream>
#include <array>

template<typename... Ts>
constexpr auto make_array(Ts&&... ts)
    -> std::array<std::common_type_t<Ts...>,sizeof...(ts)>
{
    return { std::forward<Ts>(ts)... };
}

int main()
{
    auto b = make_array(1, 2, 3);
    std::cout << b.size() << '\n';
    for (auto i : b)
        std::cout << i << ' ';
}

See also

Favorite site