jeudi 22 février 2018

Discriminated union of recursive type

I read about discriminated unions in C++ here: http://www.stroustrup.com/C++11FAQ.html#unions

What if I wanted to make a union of recursive types?

For example, consider the following:

class Obj {
  enum class Type { kInt, kVec } type;
  union {
    int i;
    std::vector<Obj> v;
  };
  Obj& operator=(const Obj& o) { ... }
};

In this case the compiler complains about trying to use incomplete object Obj due to the recursive reference. How to work around this in a clean way?

Thanks

Aucun commentaire:

Enregistrer un commentaire