Hana BOOST_HANA_DEFINE_STRUCT不适用于std :: unique_ptr

时间:2018-07-20 18:04:05

标签: c++ boost boost-hana

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)...}; }

1 个答案:

答案 0 :(得分:2)

是的,不幸的是,members构成一个元组,当作为左值引用传递值时,它们会复制

您可以使用accessorskeys获取对每个成员的引用:

#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;
}