variant (C++ 模板元编程)

std::variant

可以理解为一个会自动清除空间的union,保证了赋值时内存的正确性,能够自动进行析构。

通过get可传入下标或者type来获取值,但是不安全,如果传入类型于当前类型不一致时会引发错误。

可以通过get_if传入下标或者值和variant指针来安全获得值。

有类模板variant_alternative来获取第几个属性的type,以及类模板variant_size来获取variant中存放了多少个属性。

  • variant
  • get< type >
  • get< N >
  • variant_alternative
  • variant_size
  • operator =
int main() {std::variant<int, float> a;a = 1;auto int_value = std::get<int>(a);std::cout << int_value << "\n";try {auto float_value = std::get<float>(a);} catch (const std::bad_variant_access &e) {std::cout << e.what() << "\n";}std::cout << std::holds_alternative<int>(a) << " "<< std::holds_alternative<float>(a) << "\n";a = 1.1f;std::cout << std::holds_alternative<int>(a) << " "<< std::holds_alternative<float>(a) << "\n";std::cout << std::get<float>(a) << " " << std::get<1>(a) << "\n";// std::cout << std::get<int>(a) << " " << std::get<0>(a) << "\n";std::cout << std::get_if<0>(&a) << " " << std::get_if<int>(&a) << "\n";std::cout << std::get_if<1>(&a) << " " << std::get_if<float>(&a) << "\n";std::variant_alternative<1, decltype(a)>::type f = 1.2;std::cout << f << "\n";std::cout << std::variant_size_v<decltype(a)> << "\n";return 0;
}

sample::variant

存储结构

union模板一层一层嵌套,即可得到我们的variant存储结构,整体的内存占用情况为sizeof(largestType<Ts...>)

template<typename ...Ts> union __union;template<typename T, typename ...Ts>
union __union<T, Ts...> {using type = __union;using rest_type = __union<Ts...>;using value_type = T;T value_;rest_type rest_;template<typename Tv>requires std::is_same_v<std::decay_t<Tv>, T>__union(Tv&& value) : value_(std::forward<Tv>(value)) {}template <typename Tv>__union(Tv&& rest) : rest_(std::forward<Tv>(rest)) {}__union() {}~ __union() {}
};template<typename T>
union __union<T> {using type = __union;using value_type = T;T value_;template<typename Tv>requires std::is_same_v<std::decay_t<Tv>, T>__union(Tv&& value) : value_(std::forward<Tv>(value)) {}__union() {}~ __union() {}
};

简单测试访问一下看看:

int a = 114;
auto u1 = __union<short int, int, unsigned int, long long, float, double>(a);
std::cout << u1.value_ << " " << u1.rest_.value_ << " " << u1.rest_.rest_.value_ << " "<< u1.rest_.rest_.rest_.value_ << " " << u1.rest_.rest_.rest_.rest_.value_ << " "<< u1.rest_.rest_.rest_.rest_.rest_.value_ << "\n";const double b = 115.514;
auto u2 = __union<short int, int, unsigned int, long long, float, double>(b);
std::cout << u2.value_ << " " << u2.rest_.value_ << " " << u2.rest_.rest_.value_ << " "<< u2.rest_.rest_.rest_.value_ << " " << u2.rest_.rest_.rest_.rest_.value_ << " "<< u2.rest_.rest_.rest_.rest_.rest_.value_ << "\n";std::string str = "lifehappy";
auto u3 = __union<int, double, std::string>(std::move(str));
std::cout << u3.value_ << " " << u3.rest_.value_ << " " << u3.rest_.rest_.value_ << " : __union\n";
std::cout << str << " : str\n";

114 114 114 4294967410 1.59748e-43 2.122e-314
-30409 1614907703 1614907703 4637828992051808567 5.5783e+19 115.514
1701210476 4.06896e+233 lifehappy : __union
: str

variant

template<typename ...Ts>
struct variant {using type = variant;using data_type = __union<Ts...>;__union<Ts...> data_;template<typename Tv>variant(Tv&& data) : data_(std::forward<Tv>(data)) {}variant() {}~ variant() {}
};

variant_alternative

template<int N, typename ...Ts> struct variant_alternative {};template<int N, typename ...Ts>
struct variant_alternative<N, variant<Ts...>>: variant_alternative<N, typename variant<Ts...>::data_type> {};template<int N, typename ...Ts>
struct variant_alternative<N, __union<Ts...>>: variant_alternative<N - 1, typename __union<Ts...>::rest_type> {};template<typename ...Ts>
struct variant_alternative<0, __union<Ts...>> {using type = __union<Ts...>::value_type;
};template<int N, typename ...Ts>
using variant_alternative_t = variant_alternative<N, Ts...>::type;

variant_size

template<typename ...Ts> struct variant_size {};template<typename ...Ts>
struct variant_size<variant<Ts...>>: std::integral_constant<int, sizeof...(Ts)> {};template<typename ...Ts>
constexpr static int variant_size_v = variant_size<Ts...>::value;

get< type > 、get< N >

这里的实现并不会像std::variant一样,即可以把我们的实现认为就是一个union

get< N >

template<int N, typename T>
struct get_impl {static auto&& get(T &data) {return get_impl<N - 1, typename T::rest_type>::get(data.rest_);}
};template<typename T>
struct get_impl<0, T> {static T::value_type& get(T &data) {return data.value_;}
};template<int N, typename T>
static auto&& get(T &var) {return get_impl<N, typename T::data_type>::get(var.data_);
}

get< type >

template<typename T, typename Tv>
struct get_type_impl {static auto&& get(Tv &data) {return get_type_impl<T, typename Tv::rest_type>::get(data.rest_);}
};template<typename Tv>
struct get_type_impl<typename Tv::value_type, Tv> {static Tv::value_type& get(Tv &data) {return data.value_;}
};template<typename T, typename Tv>
static auto&& get(Tv &var) {return get_type_impl<T, typename Tv::data_type>::get(var.data_);
}

operator =

这里也是variant最重要的功能了,能够在赋值的时候自动析构原来保存的值。

先看不加析构函数的版本:

template<typename Tv>
variant& operator = (Tv&& data) {new (&data_) data_type(std::forward<Tv>(data));return *this;
}

TEST

struct Test {~ Test() {std::cout << "~ Test()\n";}
};Test a, b;
variant<int, long long, Test> variant_test(a);
std::cout << "OK\n";
variant_test = b;
std::cout << "OK\n";

OK
OK
~ Test()
~ Test()

只有最后a、b的两次析构,缺少了赋值时和销毁variant时的析构调用。

要能够析构,那么势必我们需要保存当前的type,为了方便,这里直接使用一个int变量来保存type所对应的下标,

同时实现一个类模板,获取当前值在列表中的位置。

template<typename Tu, typename T> struct type_index_impl: std::integral_constant<int, type_index_impl<typename Tu::rest_type, T>::value + 1> {};template<typename Tu>
struct type_index_impl<Tu, typename Tu::value_type>: std::integral_constant<int, 0> {};template<typename Tu, typename T>
constexpr static int type_index = type_index_impl<Tu, T>::value;

得到所有类型的析构函数,由于variant的类型是动态加载的,考虑将所有类型的destructor存下来,按需调用:

std::function<void(void *)> destructors[sizeof...(Ts)] ={ [](void *ptr) { static_cast<Ts*>(ptr)->~Ts(); }... };

接着稍微修改一下operator =、~variant()

template<typename Tv>
variant& operator = (Tv&& data) {if (~type_) {destructors[type_](&data_);}new (&data_) data_type(std::forward<Tv>(data));type_ = type_index<data_type, std::decay_t<Tv>>;return *this;
}~ variant() {if (~type_) {destructors[type_](&data_);}
}

TEST

struct Test1 {~ Test1() {std::cout << "~ Test1()\n";}
}a;struct Test2 {~ Test2() {std::cout << "~ Test2()\n";}
}b;variant<int, long long, Test1, Test2> variant_test(a);
std::cout << "OK\n";
variant_test = b;
std::cout << "OK\n";
OK
Test1()
OK
Test2()
Test2()
Test1()

Code

#include <iostream>
#include <string>
#include <type_traits>
#include <functional>template<typename ...Ts> union __union;template<typename T, typename ...Ts>
union __union<T, Ts...> {using type = __union;using rest_type = __union<Ts...>;using value_type = T;T value_;rest_type rest_;template<typename Tv>requires std::is_same_v<std::decay_t<Tv>, T>__union(Tv&& value) : value_(std::forward<Tv>(value)) {}template <typename Tv>__union(Tv&& rest) : rest_(std::forward<Tv>(rest)) {}__union() {}~ __union() {}
};template<typename T>
union __union<T> {using type = __union;using value_type = T;T value_;template<typename Tv>requires std::is_same_v<std::decay_t<Tv>, T>__union(Tv&& value) : value_(std::forward<Tv>(value)) {}__union() {}~ __union() {}
};template<typename Tu, typename T> struct type_index_impl: std::integral_constant<int, type_index_impl<typename Tu::rest_type, T>::value + 1> {};template<typename Tu>
struct type_index_impl<Tu, typename Tu::value_type>: std::integral_constant<int, 0> {};template<typename Tu, typename T>
constexpr static int type_index = type_index_impl<Tu, T>::value;template<typename ...Ts>
struct variant {using type = variant;using data_type = __union<Ts...>;__union<Ts...> data_;int type_{-1};std::function<void(void *)> destructors[sizeof...(Ts)] ={ [](void *ptr) { static_cast<Ts*>(ptr)->~Ts(); }... };template<typename Tv>variant(Tv&& data) : data_(std::forward<Tv>(data)),type_(type_index<data_type, std::decay_t<Tv>>) {}template<typename Tv>variant& operator = (Tv&& data) {if (~type_) {destructors[type_](&data_);}new (&data_) data_type(std::forward<Tv>(data));type_ = type_index<data_type, std::decay_t<Tv>>;return *this;}variant() {}~ variant() {if (~type_) {destructors[type_](&data_);}}
};template<int N, typename ...Ts> struct variant_alternative {};template<int N, typename ...Ts>
struct variant_alternative<N, variant<Ts...>>: variant_alternative<N, typename variant<Ts...>::data_type> {};template<int N, typename ...Ts>
struct variant_alternative<N, __union<Ts...>>: variant_alternative<N - 1, typename __union<Ts...>::rest_type> {};template<typename ...Ts>
struct variant_alternative<0, __union<Ts...>> {using type = __union<Ts...>::value_type;
};template<int N, typename ...Ts>
using variant_alternative_t = variant_alternative<N, Ts...>::type;template<typename ...Ts> struct variant_size {};template<typename ...Ts>
struct variant_size<variant<Ts...>>: std::integral_constant<int, sizeof...(Ts)> {};template<typename ...Ts>
constexpr static int variant_size_v = variant_size<Ts...>::value;template<int N, typename T>
struct get_n_impl {static auto&& get(T &data) {return get_n_impl<N - 1, typename T::rest_type>::get(data.rest_);}
};template<typename T>
struct get_n_impl<0, T> {static T::value_type& get(T &data) {return data.value_;}
};template<int N, typename T>
static auto&& get(T &var) {return get_n_impl<N, typename T::data_type>::get(var.data_);
}template<typename T, typename Tv>
struct get_type_impl {static auto&& get(Tv &data) {return get_type_impl<T, typename Tv::rest_type>::get(data.rest_);}
};template<typename Tv>
struct get_type_impl<typename Tv::value_type, Tv> {static Tv::value_type& get(Tv &data) {return data.value_;}
};template<typename T, typename Tv>
static auto&& get(Tv &var) {return get_type_impl<T, typename Tv::data_type>::get(var.data_);
}int main() {int a = 114;auto u1 = __union<short int, int, unsigned int, long long, float, double>(a);std::cout << u1.value_ << " " << u1.rest_.value_ << " " << u1.rest_.rest_.value_ << " "<< u1.rest_.rest_.rest_.value_ << " " << u1.rest_.rest_.rest_.rest_.value_ << " "<< u1.rest_.rest_.rest_.rest_.rest_.value_ << "\n";const double b = 115.514;auto u2 = __union<short int, int, unsigned int, long long, float, double>(b);std::cout << u2.value_ << " " << u2.rest_.value_ << " " << u2.rest_.rest_.value_ << " "<< u2.rest_.rest_.rest_.value_ << " " << u2.rest_.rest_.rest_.rest_.value_ << " "<< u2.rest_.rest_.rest_.rest_.rest_.value_ << "\n";std::string str1 = "lifehappy";auto u3 = __union<int, double, std::string>(std::move(str1));std::cout << u3.value_ << " " << u3.rest_.value_ << " " << u3.rest_.rest_.value_ << " : __union\n";std::cout << str1 << " : str\n";auto v3 = variant<int, double, std::string>();std::cout << std::is_same_v<int, variant_alternative_t<0, decltype(v3)>> << " "<< std::is_same_v<double, variant_alternative_t<1, decltype(v3)>> << " "<< std::is_same_v<std::string, variant_alternative_t<2, decltype(v3)>> << "\n";std::cout << std::is_same_v<int, variant_alternative_t<0, decltype(u3)>> << " "<< std::is_same_v<double, variant_alternative_t<1, decltype(u3)>> << " "<< std::is_same_v<std::string, variant_alternative_t<2, decltype(u3)>> << "\n";std::cout << std::is_same_v<variant_alternative_t<0, decltype(v3)>, variant_alternative<0, decltype(v3)>::type> << " "<< std::is_same_v<variant_alternative_t<1, decltype(v3)>, variant_alternative<1, decltype(v3)>::type> << " "<< std::is_same_v<variant_alternative_t<2, decltype(v3)>, variant_alternative<2, decltype(v3)>::type> << "\n";std::cout << std::is_same_v<variant_alternative_t<0, decltype(u3)>, variant_alternative<0, decltype(u3)>::type> << " "<< std::is_same_v<variant_alternative_t<1, decltype(u3)>, variant_alternative<1, decltype(u3)>::type> << " "<< std::is_same_v<variant_alternative_t<2, decltype(u3)>, variant_alternative<2, decltype(u3)>::type> << "\n";std::cout << variant_size<decltype(v3)>::value << " " << variant_size_v<decltype(v3)> << "\n";variant<int, unsigned int, long long, double, std::string> v4((int)114514);std::cout << get_n_impl<0, decltype(v4.data_)>::get(v4.data_) << " " << get_n_impl<1, decltype(v4.data_)>::get(v4.data_) << " "<< get_n_impl<2, decltype(v4.data_)>::get(v4.data_) << " " << get_n_impl<3, decltype(v4.data_)>::get(v4.data_) << " "<< get_n_impl<4, decltype(v4.data_)>::get(v4.data_) << "\n";std::cout << get<0>(v4) << " " << get<1>(v4) << " " << get<2>(v4) << " " << get<3>(v4) << " " << get<4>(v4) << "\n";std::cout << get_type_impl<int, decltype(v4.data_)>::get(v4.data_) << " " << get_type_impl<unsigned int, decltype(v4.data_)>::get(v4.data_) << " "<< get_type_impl<long long, decltype(v4.data_)>::get(v4.data_) << " " << get_type_impl<double, decltype(v4.data_)>::get(v4.data_) << " "<< get_type_impl<std::string, decltype(v4.data_)>::get(v4.data_) << "\n";std::cout << get<int>(v4) << " " << get<unsigned int>(v4) << " " << get<long long>(v4) << " " << get<double>(v4) << " " << get<std::string>(v4) << "\n";int int1 = 1;const int int2 = 1;int &int3 = int1;std::cout << variant<int, float, std::string>().type_ << " "<< variant<int, float, std::string>(int1).type_ << " "<< variant<int, float, std::string>(int2).type_ << " "<< variant<int, float, std::string>(int3).type_ << " "<< variant<int, float, std::string>(1.1f).type_ << " "<< variant<int, float, std::string>(std::string("lifehappy")).type_ << "\n";struct Test1 {~ Test1() {std::cout << "~ Test1()\n";}}ta;struct Test2 {~ Test2() {std::cout << "~ Test2()\n";}}tb;variant<int, long long, Test1, Test2> variant_test(ta);std::cout << "OK\n";variant_test = tb;std::cout << "OK\n";return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/130315.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

竞赛 深度学习疫情社交安全距离检测算法 - python opencv cnn

文章目录 0 前言1 课题背景2 实现效果3 相关技术3.1 YOLOV43.2 基于 DeepSort 算法的行人跟踪 4 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; **基于深度学习疫情社交安全距离检测算法 ** 该项目较为新颖&#xff0c;适合作为竞赛…

Linux mount 命令于的基本用法与 umount 的命令

1. 用 Linux mount/umount 能做什么&#xff1f; 不同的操作系统使用不同的文件系统格式。MS-DOS 支持 FAT16 文件系统&#xff0c;Windows98 支持 FAT16、FAT32 文件系 统&#xff0c;WindowsNT 支持 FAT16、NTFS 文件系统&#xff0c;Windows2000 则支持 FAT16、FAT32、NTFS…

【网络奇遇记】那年我与计算机网络的初相识

&#x1f308;个人主页&#xff1a;聆风吟 &#x1f525;系列专栏&#xff1a;《网络奇遇记》 &#x1f516;少年有梦不应止于心动&#xff0c;更要付诸行动。 文章目录 一. 信息时代的计算机网络二. 计算网络的定义和分类三. 计算机网络的特点四. 计算机网路在信息时代的应用五…

图解Linux进程优先级

目录 1.什么是进程优先级&#xff1f; 2.进程优先级原理 3.查看进程优先级 4.修改进程优先级 4.1 setpriority函数原型 4.2 getpriority函数原型 4.3 sched_setscheduler函数原型 4.4 sched_getscheduler函数原型 4.5 sched_setparam函数原型 4.6 sched_getparam函数…

Unity Perception合成数据生成、标注与ML模型训练

在线工具推荐&#xff1a; Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 3D场景编辑器 任何训练过机器学习模型的人都会告诉你&#xff0c;模型是从数据得到的&#xff0c;一般来说&#xff0c;更多的数据和标签会带来更好的性能。 …

限制LitstBox控件显示指定行数的最新数据(3/3)

实例需求&#xff1a;由于数据行数累加增加&#xff0c;控件加载的数据越来越多&#xff0c;每次用户都需要使用右侧滚动条拖动才能查看最新数据。 因此希望ListBox只加载最后10行数据&#xff08;不含标题行&#xff09;&#xff0c;这样用户可以非常方便地选择数据&#xff…

JMeter组件

1.JMeter常用组件 必须组件&#xff1a;测试计划&#xff0c;线程组&#xff08;包含多个线程&#xff09;&#xff0c;取样器 测试计划&#xff0c;JMeter默认创建且仅有一个 线程组&#xff1a; 添加步骤&#xff1a; 选择TestPlan并点击鼠标右键添加 分类以及使用&…

Spring Data Redis + RabbitMQ - 基于 string 实现缓存、计数功能(同步数据)

目录 一、Spring Data Redis 1.1、缓存功能 1.1.1、分析 1.1.2、案例实现 1.1.3、效果演示 1.2、计数功能&#xff08;Redis RabbitMQ&#xff09; 1.2.1、分析 1.2.2、案例实现 一、Spring Data Redis 1.1、缓存功能 1.1.1、分析 使用 redis 作为缓存&#xff0c; M…

curl(四)证书相关

一 证书相关 ① -k 1、客户端忽略服务端证书校验 -k | --insecure --> 单向[1]、这个选项显式地允许curl 执行不安全 的SSL连接和传输[2]、所有SSL连接都试图通过使用默认安装的CA证书捆绑包来确保安全[3]、这使得所有被认为是不安全的连接失败,除非使用-k --> 自签…

顺序表(数据结构)

顺序表是用一段 物理地址连续 的存储单元依次存储数据元素的线性结构&#xff0c;一般情况下采用数组存 储。在数组上完成数据的增删查改。 顺序表的实现 #pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h> typedef int SLDataType;//…

k8s的RBAC中,clusterrole, rolebinding 是什么关系谁先谁后

在Kubernetes中&#xff0c;Role-Based Access Control&#xff08;RBAC&#xff09;用于控制集群中不同用户、服务账号或组的访问权限。ClusterRole 和 RoleBinding 是两个关键的 RBAC 组件&#xff0c;它们之间的关系是 ClusterRole 定义了一组权限规则&#xff0c;而 RoleBi…

一座 “数智桥梁”,华为助力“天堑变通途”

《水调歌头游泳》中的一句话&#xff0c;“一桥飞架南北&#xff0c;天堑变通途”&#xff0c;广为人们所熟知&#xff0c;其中展现出的&#xff0c;是中国人对美好出行的无限向往。 天堑变通途从来不易。 中国是当今世界上交通运输最繁忙、最快捷的国家之一&#xff0c;交通行…

2023-在mac下安装Homebrew的国内镜像

mac安装Homebrew的国内镜像 尝试使用其他下载源&#xff1a;GitHub 可能会受到访问限制&#xff0c;尝试使用其他镜像或下载源。您可以使用清华大学、中科大或阿里云的 Homebrew 镜像&#xff0c;以提高下载速度和可靠性。例如&#xff0c;可以使用阿里云的镜像来安装 Homebre…

冒泡排序(Java)

基本思想 比较前后相邻的二个数据&#xff0c;如果前面数据大于后面的数据&#xff0c;就将这二个数据交换。这样对数组的第 0 个数据到 N-1 个数据进行一次遍历后&#xff0c;最大的一个数据就“沉”到数组第N-1 个位置。如此循环 (N-1)次&#xff0c;每次循环需要比较的个数…

任务1 部署ChatGLM3-6B大模型并进行对话测试

部署ChatGLM3-6B大模型并进行对话测试 0 介绍&#xff1a;1 趋动云项目创建与环境配置1.1 创建项目&#xff1a;1.2 配置环境1.2.1 进入终端1.2.2 设置镜像源1.2.3 克隆项目,并安装依赖 2 修改代码&#xff0c;改路径以及启动代码3 运行代码3.1 运行gradio界面&#xff1a;3.2 …

雷池WAF社区版的使用教程

最近听说了一款免费又好用的WAF软件&#xff0c;雷池社区版&#xff0c;体验了一下虽然还有很多改进的空间 但是总体来说很适合小站长使用&#xff0c;和学习使用 也建议所有想学防火墙和红队&#xff08;攻击队&#xff09;练习使用&#xff0c;听说给官网提交绕过还有额外的…

ZKP Introduction of Nova (Yu Guo) 手写笔记

ZKP学习笔记 郭宇老师Nova课程手写笔记

你知道Python、Pycharm、Anaconda 三者之间的关系吗?

哈喽~大家好呀 Python作为深度学习和人工智能学习的热门语言&#xff0c;你知道Python、Pycharm、Anaconda 三者之间的关系吗&#xff1f;学习一门语言&#xff0c;除了学会其简单的语法之外还需要对其进行运行和实现&#xff0c;才能实现和发挥其功能和作用。下面来介绍运行P…

机器学习(深度学习)轴承故障诊断分类(提供故障数据和python代码实现)

机器学习&#xff08;深度学习&#xff09;故障诊断分类&#xff08;提供故障数据和python代码实现&#xff09; 轴承故障数据集和python代码自取&#xff1a;https://mbd.pub/o/bread/ZZWTm5hw 摘要&#xff1a;机器学习广泛的应用于机械故障诊断和故障分类问题&#xff0c;本…

时间复杂度的计算技巧-算法模型中的时间复杂度如何计算,有哪些技巧呢

大家好&#xff0c;我是微学AI&#xff0c;今天给大家介绍一下时间复杂度的计算技巧-算法模型中的时间复杂度如何计算&#xff0c;有哪些技巧呢&#xff0c;算法的时间复杂度是评估算法性能和效率的一种方式&#xff0c;它表示算法需要执行多少次基本操作才能完成其任务&#x…