Reputation: 26276
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.
Upvotes: 10
Views: 2715
Reputation: 170084
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
.
Upvotes: 9