May I change the held type in a std::variant from within a call to std::visit

折月煮酒 提交于 2019-11-30 10:59:43

The code is fine.

There is no requirement in the specification of std::visit that the visitor not change the alternative of any of the variants it is invoked on. The only requirement is:

Requires: For each valid pack m, e(m) shall be a valid expression. All such expressions shall be of the same type and value category; otherwise, the program is ill-formed.

Your visitor is a valid expression for each m and always returns void, so it satisfies the requirements and has well-defined behavior.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!