Tom
Tom

Reputation: 1291

Boost::variant with a object which references the same variant

How can I have a variant of different objects A, B, C where C has a reference to the variant?

class A {
   ...
};

class B {
   ...
};

class C {
   ...
   std::vector<PossibleValues> storage; // reference to variant
   ...
};

boost::variant<A, B, C> PossibleValues;

Upvotes: 0

Views: 71

Answers (1)

Jarod42
Jarod42

Reputation: 217085

With correct order, you might have:

class A {
   //...
};

class B {
   //...
};

class C;
using PossibleValues = std::variant<A, B, C>;

class C {
   //...
   std::vector<PossibleValues> storage; // reference to variant
   //...
};

Demo

Upvotes: 1

Related Questions