重写类型以使用boost :: recursive_wrapper而不是boost :: make_recursive变体

时间:2014-10-18 02:33:47

标签: c++ boost boost-variant

我有这种类型

using expression = boost::make_recursive_variant<
    number,
    std::tuple<
        boost::recursive_variant_,
        binary_operator,
        boost::recursive_variant_
    >
>;

binary_operator是什么并不重要,这种类型因为这个而无法正常工作 Using boost::make_recursive_variant with tuple

编辑:实际上我不知道为什么它不起作用。事实证明,在我链接的问题的答案中提到的拉取请求已合并为提升1.56.0,这意味着问题出在其他地方。这是一个显示问题的最小程序:

#include <boost/variant.hpp>
#include <boost/variant/recursive_variant.hpp>

#include <tuple>

struct A {};
struct B {};

using working_variant = boost::variant<
    A,
    B
>;

using not_working_variant = boost::make_recursive_variant<
    A,
    std::tuple<
        boost::recursive_variant_,
        B,
        boost::recursive_variant_
    >
>;

int main() {
    working_variant x = A();
    not_working_variant y = A();
}

编译错误:

$ clang++ -stdlib=libc++ -std=c++14 -isystem ~/soft/boost_1_56_0/ min.cpp 
min.cpp:25:25: error: no viable conversion from 'A' to 'not_working_variant' (aka 'make_recursive_variant<A, std::tuple<boost::recursive_variant_, B,
      boost::recursive_variant_> >')
    not_working_variant y = A();
                        ^   ~~~
/home/crabman/soft/boost_1_56_0/boost/variant/recursive_variant.hpp:176:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from
      'A' to 'const boost::make_recursive_variant<A, std::__1::tuple<boost::recursive_variant_, B, boost::recursive_variant_>, boost::detail::variant::void_,
      boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_,
      boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_,
      boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_,
      boost::detail::variant::void_, boost::detail::variant::void_> &' for 1st argument
struct make_recursive_variant
       ^
/home/crabman/soft/boost_1_56_0/boost/variant/recursive_variant.hpp:176:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from
      'A' to 'boost::make_recursive_variant<A, std::__1::tuple<boost::recursive_variant_, B, boost::recursive_variant_>, boost::detail::variant::void_,
      boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_,
      boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_,
      boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_,
      boost::detail::variant::void_, boost::detail::variant::void_> &&' for 1st argument
struct make_recursive_variant
       ^
1 error generated.

所以我想我应该重写它来使用boost :: recursive_wrapper。但是我该怎么做?我不能向前声明一个类型,它将使用&#34;使用&#34;来定义。或&#34; typedef&#34;,我可以吗?

1 个答案:

答案 0 :(得分:1)

无视我在问题中所写的一切。事实证明你需要使用:: type来定义一些带有boost :: make_recursive_variant的类型:

using working_variant = boost::variant<
    A,
    B
>;

using working_recursive_variant = boost::make_recursive_variant<
    A,
    B
>::type;

即使对于具有实际递归和使用std :: tuple的代码也是如此。