Search code examples
c++boostc++14boost-variantstd-variant

Is there boost::visit like std::visit, for boost::variant?


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.


Solution

  • 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.