C++ 使用模板参数定义Hana结构
有没有办法定义(调整)具有模板参数的Hana结构 是一个非模板类C++ 使用模板参数定义Hana结构,c++,templates,boost-fusion,boost-hana,C++,Templates,Boost Fusion,Boost Hana,有没有办法定义(调整)具有模板参数的Hana结构 是一个非模板类 #include <boost/hana/define_struct.hpp> #include <string> namespace hana = boost::hana; struct Person { BOOST_HANA_DEFINE_STRUCT(Person, (std::string, name), (int, age) ); }; #包括
#include <boost/hana/define_struct.hpp>
#include <string>
namespace hana = boost::hana;
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person,
(std::string, name),
(int, age)
);
};
#包括
#包括
名称空间hana=boost::hana;
结构人{
增强结构(人、,
(std::string,name),
(国际,年龄)
);
};
我们尝试添加模板参数,但出现编译错误:
template<class S = std::string, class I = int>
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person<S, I>,
(S, name),
(I, age)
);
};
模板
结构人{
增强结构(人、,
(S,姓名),
(一、年龄)
);
};
我认为这失败是因为使用了逗号,所以我尝试用decltype(Person)
代替Person
在Boost.Fusion中我们有,但我在Hana中找不到等效的
如何使用模板参数定义Hana结构?我在这里找到了解决方案:
模板
结构人{
姓名;
I年龄;
结构hana\u访问器\u impl{
静态BOOST\u HANA\u CONSTEXPR\u LAMBDA自动应用(){
返回boost::hana::make_tuple(
boost::hana::make_pair(boost_-hana_字符串(“名称”),
[](自动和&p)->decltype(自动){
返回boost::hana::id(std::forward(p).name);
}),
boost::hana::make_pair(boost_-hana_字符串(“年龄”),
[](自动和&p)->decltype(自动){
返回boost::hana::id(std::forward(p).age);
})
);
}
};
};
这引发了另一个问题,为什么Hana需要第一个参数?既然没有必要
顺便说一句,这也奏效了,这是我一开始没有尝试过的。
我不确定它是否普遍有效
template<class S, class I>
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person,
(std::string, name),
(int, age)
);
};
模板
结构人{
增强结构(人、,
(std::string,name),
(国际,年龄)
);
};
宏可能需要类名作为指向成员的指针(例如&Person::name)。@JasonRice,这可以解释为什么它不应该有额外的模板参数。实际上,它应该与中的模板参数一起工作。我不知道。它必须是一个模板的实例化类型,比如Person
。
template<class S, class I>
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person,
(std::string, name),
(int, age)
);
};