Hana BOOST_HANA_DEFINE_STRUCT 不适用于 std::unique_ptr

Hana BOOST_HANA_DEFINE_STRUCT does not work with std::unique_ptr

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

是的,不幸的是,members 生成一个元组,其中的值在作为左值引用传递时被 复制

您可以使用 accessors or 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;
}