c++ - 对于 boost::variant,是否有 boost::visit 之类的 std::visit?

标签 c++ boost c++14 boost-variant std-variant

使用 C++14,我使用 boost::variant作为编译时多态的一种方式:

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

这两个类都有一个方法 sayHello() .我想调用:
MyType obj = ...; // either A() or B()
boost::visit([](auto&& o) { o.sayHello();}, obj);

我知道 static_visitor 方式,但我觉得它很麻烦。有没有boost::visit喜欢 std::visit 我失踪了吗?如果没有,为什么它不存在?

最小示例 here .

最佳答案

有,但它叫 boost::apply_visitor .其与 boost::variant 相关的行为是作为 std::visitstd::variant .

关于c++ - 对于 boost::variant,是否有 boost::visit 之类的 std::visit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183917/

相关文章:

c++ - Boost ASIO 将 X 字节同步读入 vector

c++ - 从 Howard Hinnant 的日期库中提取日期和时间

c++ - boost::condition_variable::notify_one() 的并发性

c++ - 为什么我在包含加载程序 header 时无法使用 OpenGL 函数?

C++ 终止等待 std::thread

c++ - 关于STL线程安全和STL调试的问题

c++ - 使用正确的位大小在 C++ 中将十进制转换为二进制

c++ - 默认赋值运算符可以访问基类的私有(private)成员

c++ - 如何编写带有参数的函数,其中类型是用 'auto' 字推导的?

c++ - VS 2017 : fail to create C++ projects