C++17类型std::variant介绍

std::variant代表了类型安全的union类型,与union类似,std::variant的一个实例要么存储了可选类型中的一个值,要么没有存储。但与union比,其优势是可以判断当前真实的存储类型并且可以存储结构体这种复杂的数据结构。下面直接给一个实例:

#include <variant>
#include <string>
#include<cassert>
#include<iostream>
int main() {
	// 定义两个variant类型实例
	std::variant<int, float> v, w;
	v = 18;
	// index 可以判断实际对应的类型,,默认第一种类型。
	std::cout << "Corresponding type is " << w.index() << std::endl;
	int i = std::get<int>(v);
	std::cout << "value is " << i << std::endl;
	w = std::get<int>(v);
	w = std::get<0>(v); // 与上一行功能相同
	w = v; // 与上一行功能相同
	// 类型对不上会抛出异常
	try {
		std::get<float>(w);
	}
	catch (const std::bad_variant_access& ex) {
		std::cout << ex.what() << std::endl;
	}
	// 避免使用 try catch,可以使用get_if
	float* fres = std::get_if<float>(&v);
	if (fres) {
		std::cout << "float value is " << *fres << std::endl;
	}
	else {
		std::cout << "does not contain a value" << std::endl;
	}
	v = 1.23f;
	fres = std::get_if<float>(&v);
	if (fres) {
		std::cout << "float value is " << *fres << std::endl;
	}
	else {
		std::cout << "does not contain a value" << std::endl;
	}
	// index 可以判断实际对应的类型
	std::cout << "Corresponding type is " << w.index() << std::endl;
	// holds_alternative 判断当前是否使用此类型
	if (std::holds_alternative<float>(v)) {
		std::cout << "now using float type" << std::endl;
	}
	else {
		std::cout << "not using float type" << std::endl;
	}
	return 0;
}

执行结果如下:

Corresponding type is 0
value is 18
bad variant access
does not contain a value
float value is 1.23
Corresponding type is 0
now using float type

可以看到查用的函数,get可以获取指定类型的数据,如果当前存储的不是此类型则会抛异常,为了解决此问题可以catch此异常,或者使用get_if通过判断指针是否为空来获取,holds_alternative可以用来判断当前是否存储的是该类型数据。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值