假设我有以下结构:
struct MyStruct
{
int field1;
float field2;
};我想获得结构中使用boost-hana的字段数。
#include <boost/hana/adapt_struct.hpp>
BOOST_HANA_ADAPT_STRUCT(MyStruct, position, field1, field2);
// this code does not work:
constexpr std::size_t NUMBER_OF_FIELDS = boost::hana::length<MyStruct>();
static_assert(NUMBER_OF_FIELDS == 2);如何在boost-hana适应结构中获得字段数?
发布于 2022-07-09 20:42:47
Hana的具体目标是通过将类型函数提升到constexpr域来简化元编程。在简单的英语中:您不应该使用length<>作为“类型函数模板”,而应该使用它作为一个正常的函数:
住在Coliru
struct MyStruct
{
int position;
int field1;
float field2;
};
#include <boost/hana.hpp>
BOOST_HANA_ADAPT_STRUCT(MyStruct, position, field1, field2);
constexpr std::size_t NUMBER_OF_FIELDS = boost::hana::size(MyStruct{});
static_assert(NUMBER_OF_FIELDS == 3);
int main(){}https://stackoverflow.com/questions/72920937
复制相似问题