把variant类型作为参数传递给一个函数,并不像想象的那么简单:
- #include
- #include
- #include
- using namespace std;
-
- void pOut(variant<int, double, string>& v)
- {
- if(auto p = get_if<int>(&v))
- {
- cout<<"v is int, and value is "<<*p<
- }
- else if(auto p = get_if<double>(&v))
- {
- cout<<"v is double, and value is "<<*p<
- }
- else if(auto p = get_if
(&v)) - {
- cout<<"v is string, and value is "<<*p<
- }
- }
-
- int main()
- {
- variant<int, double, string> v1{8}, v2{3.14}, v3{"hi"};
- pOut(v1);
- pOut(v2);
- pOut(v3);
- return 0;
- }
运行程序输出:
v is int, and value is 8
v is double, and value is 3.14
v is string, and value is hi
可以看到需要在函数里对variant当前的类型进行检查,
-
相关阅读:
华为机试 - 判断一组不等式是否满足约束并输出最大差
docker安装redis并搭建集群
SpringCloud无介绍快使用,sentinel服务熔断功能(二十四)
网站系统告警哪家强
【Python入门教程】Python中函数的用法和意义
6.1.2 基于MSI文件安装MySQL
常见的SQL在线练习平台
阿里云安全恶意程序检测(速通一)
LeetCode每日一题(1717. Maximum Score From Removing Substrings)
PHP+学生成绩管理系统 毕业设计-附源码201829
-
原文地址:https://blog.csdn.net/jiemashizhen/article/details/128070142