Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用模板参数定义Hana结构_C++_Templates_Boost Fusion_Boost Hana - Fatal编程技术网

C++ 使用模板参数定义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) ); }; #包括

有没有办法定义(调整)具有模板参数的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)
    );
};
#包括
#包括
名称空间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)
    );
};