I have two pointers that only one of them can be set, so I am considering using boost::variant, say: boost::variant<shared_ptr<Type1> shared_ptr<Type2>>
. Type 1 and 2 are different but they share some functionality. Thay for example, both have the method IsUnique
.
If I have the code to check the initialization:
ASSERT(type1 != nullptr || type2 != nullptr);
ASSERT(type1 == nullptr || type2 == nullptr);
ASSERT(type1 == nullptr || type1->IsUnique());
ASSERT(type2 == nullptr || type2->IsUnique());
I would expect to be able to replace it with something as close as possible to:
ASSERT(variant != nullptr);
ASSERT(variant->IsUnique());
But it seems that I have to define visitors, make switching on types.
Do I miss something, is there a template or something that will allow me to apply something to whatever the current type is? It could be c++14.