Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 700
The Quantum Physicist Avatar asked May 17 '19 09:05

The Quantum Physicist


Video Answer


1 Answers

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.

like image 91
StoryTeller - Unslander Monica Avatar answered Sep 19 '22 17:09

StoryTeller - Unslander Monica