9

With C++14, I'm using boost::variant as a way of compile-time polymorphism:

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

Both classes have a method sayHello(). I'd like to call:

MyType obj = ...; // either A() or B()
boost::visit([](auto&& o) { o.sayHello();}, obj);

I know the static_visitor way, but I find it cumbersome. Is there a boost::visit like std::visit that I'm missing? If not, why doesn't it exist?

Minimal example here.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • Have you tried using lambda instead of static visitor in apply_visitor? IRC, it should "just work"(tm). – Dan M. May 17 '19 at 09:53

1 Answers1

8

There is, but it's called boost::apply_visitor. Its behavior in relation to boost::variant is the as std::visit's to std::variant.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458