具有增强变体的Segfault

时间:2013-10-24 23:26:56

标签: c++ boost boost-variant

我已经开始构建一个小数据类型,在当前阶段,我只有一个递增类型的boost变量。事实证明,当我尝试实例化我的类型时,我得到一个段错误。如果我在递归类型之前放置一个字符串,它就会把它放在它没有的类型之后。我使用mingw和gcc 4.8.1 64bit并提升1.54。

我是否以错误的方式使用boost变体?

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

#include <string>
#include <iostream>

struct A;
struct B;

using C = boost::variant<boost::recursive_wrapper<A>>;

// works:
// using C = boost::variant<std::string, boost::recursive_wrapper<A>>;

struct A {
  std::string name;
  C variant;
};

struct B {
  std::string name;
  C variant;
};

int main() {

  std::cout << "start" << std::endl;
  B hAST; // <--- segfaults
  std::cout << "end" << std::endl;

  return 0;
}

1 个答案:

答案 0 :(得分:3)

我认为这是由于variant的“永不空”的保证:C的默认构造函数必须使用其第一个模板参数的默认构造值初始化自身 - 这是{ {1}} - recursive_wrapper<A>的默认构造函数必须使用默认构造的recursive_wrapper<A>实例初始化自身,这会导致无限递归。

假设您确实想要一个空的Avariant的实例,您可以引入一个虚拟类型作为变体的第一个参数,例如:

A

编辑:It appears you can use boost::recursive_wrapper with boost::optional,这比使用struct variant_is_empty { }; using C = boost::variant<variant_is_empty, boost::recursive_wrapper<A>>; 对于上述可选的递归类型更容易。

相关问题