把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当前的类型进行检查,
-
相关阅读:
使用Seata实现分布式事务
2022软件测试高频面试题汇总(附带答案)——建议收藏
智能优化算法常用指标一键导出为EXCEL,CEC2017函数集最优值,平均值,标准差,最差值,中位数,秩和检验,箱线图...
微信小程序部分知识点总结【2】
python 性能优化实例练习
性能测试-01-简介
基于Python3.6配置开发环境
窗口延时、侧输出流数据处理
4.网络游戏逆向分析与漏洞攻防-游戏启动流程漏洞-模拟游戏登陆器启动游戏并且完成注入
【数据结构】图的存储结构(邻接矩阵)
-
原文地址:https://blog.csdn.net/jiemashizhen/article/details/128070142