提升元组类型的 hana 大小

Boost hana size of tuple type

我知道如何在 boost::hana 中获取元组对象的大小,如下所示:

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

但是元组类型的大小呢? stl 提供了以下元组类型特征:

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

hana 中是否有相似的类型特征?

有none。我的猜测是您在这里滥用了 Hana,或者有一种等效的方法可以实现您想要实现的目标,而不必在元组类型上调用 size。但如果没有看到您的其余代码,我无法确定,所以请对此持保留态度。

解决缺少类似 tuple_size 的元函数的方法是使用 declval。你可以写:

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

请注意,根据您必须调用它的上下文,如果您有此信息,甚至可以使用 sizeof...(T)