boost variant simple call to common methods

时间秒杀一切 提交于 2019-12-10 03:31:38

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/37504740/boost-variant-simple-call-to-common-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!