Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost variant simple call to common methods

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.

like image 826
gsf Avatar asked May 28 '16 23:05

gsf


1 Answers

You might be able to just say

boost::apply_visitor([](auto const& obj) { obj.some_operation(); }, v);

in c++14 with recent boost. Let me try it out...

Yes you can: Live On Coliru

#include <boost/variant.hpp>
struct A { void some_operation() const {}; };
struct B { void some_operation() const {}; };

using Obj = boost::variant<A, B>;

int main() {
    Obj v;
    boost::apply_visitor([](auto const& obj) { obj.some_operation(); }, v);
}

A pattern I use a lot is to give the visitor an overload that handles the variant:

 struct IsNullThing {
      bool operator()(Null) const { return true; }
      template <typename T> bool operator()(T) const { return false; }

      template <typename... Ts> bool operator()(boost::variant<Ts...> const& v) const {
          return boost::apply_visitor(*this, v);
      }
 };

This way you can do:

 IsNullThing isNullThing;

 // and just call it

 MyVariant v;
 bool ok = isNullThing(v);
like image 198
sehe Avatar answered Nov 18 '22 13:11

sehe