c++boost-variant

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;

Solution

  • 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