Boost Hana的BOOST_HANA_DEFINE_STRUCT似乎不适用于std :: unique_ptr作为字段。有解决方法吗?
#include <boost/hana.hpp>
#include <memory>
struct Test
{
BOOST_HANA_DEFINE_STRUCT(Test,
(unsigned, field0),
(std::unique_ptr<unsigned>, field1));
};
int main(int argc, char** argv)
{
Test test;
boost::hana::for_each(boost::hana::members(test), [&](auto field)
{
});
return 0;
}
错误:没有匹配的构造函数来初始化'tuple :: type,typename detail :: decay>&> :: type>'(又名'tuple>>') {return {static_cast(xs)...}; }
答案 0 :(得分:2)
是的,不幸的是,members
构成一个元组,当作为左值引用传递值时,它们会复制。
您可以使用accessors
或keys
获取对每个成员的引用:
#include <boost/hana.hpp>
#include <memory>
struct Test
{
BOOST_HANA_DEFINE_STRUCT(Test,
(unsigned, field0),
(std::unique_ptr<unsigned>, field1));
};
int main(int argc, char** argv)
{
Test test;
boost::hana::for_each(boost::hana::keys(test), [&](auto key)
{
auto& field = boost::hana::at_key(test, key);
});
return 0;
}