Search code examples
c++tuplesboost-hana

Boost hana size of tuple type


I know how I can obtain the size of an tuple object in boost::hana like this:

auto tupleSize = hana::size(hana::make_tuple(1,2,3))

But what about the size of a tuple type? The stl provides allready the following tuple type trait:

constexpr size_t tupleSize = std::tuple_size<std::tuple<int, int, int>>::value;

Is there a similar type trait within hana?


Solution

  • There is none. My guess is that you are misusing Hana here, or that there's an equivalent way to do what you're trying to achieve without having to call size on a tuple type. But I can't know for sure without seeing the rest of your code, so take this with a grain of salt.

    The way you could workaround the lack of a tuple_size-like metafunction is by using declval. You could write:

    constexpr size_t tupleSize = decltype(
        hana::size(std::declval<hana::tuple<T...>>())
    )::value;
    

    Note that depending on the context in which you have to call this, it might even be possible to use sizeof...(T) if you have this information.