Is it safe to call std::visit on classes with virtual functions?
Yes, such code is required to do what you expect. When you pass a Visitor &
to std::visit
, it won't copy (and thus slice) it.
All examples of std::variant I've seen use either simple classes with no inheritance or lambdas.
That's because examples tend to be as simple as possible.