Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty braces around std::make_index_sequence

Tags:

c++

tuples

c++14

In this example on CppR:

template<typename Array, std::size_t... I>
decltype(auto) a2t_impl(const Array& a, std::index_sequence<I...>)
{
    return std::make_tuple(a[I]...);
}

template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
decltype(auto) a2t(const std::array<T, N>& a)
{
    return a2t_impl(a, Indices());
}

template<typename Func, typename Tup, std::size_t... index>
decltype(auto) invoke_helper(Func&& func, Tup&& tup, std::index_sequence<index...>)
{
    return func(std::get<index>(std::forward<Tup>(tup))...);
}

template<typename Func, typename Tup>
decltype(auto) invoke(Func&& func, Tup&& tup)
{
    constexpr auto Size = std::tuple_size<typename std::decay<Tup>::type>::value;
    return invoke_helper(std::forward<Func>(func),
                         std::forward<Tup>(tup),
                         std::make_index_sequence<Size>{});
}

there are these lines, which confuse me a lot:

std::make_index_sequence<N>
std::make_index_sequence<Size>{}

Why are the curly braces added at the end at times (and omitting them causes a compile error) and why are they sometimes not?

like image 720
marmistrz Avatar asked Feb 25 '26 05:02

marmistrz


1 Answers

The first instance (std::make_index_sequence<N>) is used in a template argument list. It's only saying that the template type parameter Indices defaults to the type std::make_index_sequence<N>. No instances of anything are created there, the use is declarative.

The second instance (std::make_index_sequence<Size>{}) is creating a default constructed instance of that type. The {} are the new C++ syntax for initialization. When the braces are empty the object will be default constructed.

like image 89
DAG Avatar answered Feb 26 '26 19:02

DAG