跟我学c++高级篇——C++26反射预览

一、c++23的反射跳票

本来按照预定,c++23中反射就应该比较全的。结果,由于众所周知的原因,线上会议肯定是不如线下会议效率高,那么反射这种对于c++不太急切的功能(当然,也有其它原因)只能向后放一放。只有盼望着下一个版本了,下一个版本就是c++26。不过c++26能否真正上线,还得看最后的结果。

二、c++26的提案

在c++26的提案中,仍然是静态反射为主。主要有下面几个功能:
A、增加反射运算符
一元运算符主要有:

1、…
2^ ::
3^ namespace-name
4^ type-id
5^ cast-expression

需要注意的,提案中只是对一些初级的表达式进行了处理,如果超出范围则反射无法完成。

B、增加拼接器(Splicers)
拼接器是[:…:],其主要的功能如下:

1[: r :] produces an expression evaluating to the entity or constant value represented by r.
2、typename[: r :] produces a simple-type-specifier corresponding to the type represented by r.
3、template[: r :] produces a template-name corresponding to the template represented by r.
4、namespace[: r :] produces a namespace-name corresponding to the namespace represented by r.
5[:r:]:: produces a nested-name-specifier corresponding to the namespace, enumeration type, or class type represented by r.
Attempting to splice a reflection value that does not meet the requirement of the splice (including “invalid reflections”) is ill-formed. For example:typename[: ^:: :] x = 0;  // Error.

拼接器还有另外一种,即Range拼接器,它主要用来处理元组等的反射。

C、增加了元信息空间
在这个功能里提供了一个命名空间std::meta::info,它主要包括以下的内容:

1、an error (corresponding to an “invalid reflection”)
2any (C++) type and type-alias
3、any function or member function
4、any variable, static data member, or structured binding
5、any non-static data member
6、any constant value
7、any template
8、any namespace

当然,这里面也有不少的限制,这里就不展开,毕竟不是正式的标准。

D、增加元功能(Metafunctions)
主要包括:

1、invalid_reflection, is_invalid, diagnose_error
2、name_of, display_name_of, source_location_of
3、type_of, parent_of, entity_of
4、template_of, template_arguments_of
5、members_of, nonstatic_data_members_of, bases_of, enumerators_of, subobjects_of
6、substitute
7、entity_ref<T>, value_of<T>, ptr_to_member<T>
8、test_type<Pred>
9、 Other Singular Reflection Predicates
10、reflect_value
11、nsdm_description, synth_struct, synth_union
12、Data Layout Reflection

这里面的细节不是太好说清楚,大家可以去看相关的提案:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2996r0.html#ref-P2670R1
省得误导大家。

三、相关的例程

1、类型和值之间的反射转换

constexpr auto r = ^int;
typename[:r:] x = 42;       // Same as: int x = 42;
typename[:^char:] c = '*';  // Same as: char c = '*';

2、成员反射

struct S { unsigned i:2, j:6; };consteval auto member_number(int n) {if (n == 0) return ^S::i;else if (n == 1) return ^S::j;else return std::meta::invalid_reflection("Only field numbers 0 and 1 permitted");
}int main() {S s{0, 0};s.[:member_number(1):] = 42;  // Same as: s.j = 42;s.[:member_number(5):] = 0;   // Error (likely with "Only field numbers 0 and 1 permitted" in text).
}

3、类型和大小的列表反射

//sizes将是一个std::array<std::size_t, 3>初始化为{sizeof(int), sizeof(float), sizeof(double)}
constexpr std::array types = {^int, ^float, ^double};
constexpr std::array sizes = []{std::array<std::size_t, types.size()> r;std::ranges::transform(types, r.begin(), std::meta::size_of);return r;
}();//产生相同的数组sizes
template<class...> struct list {};using types = list<int, float, double>;constexpr auto sizes = []<template<class...> class L, class... T>(L<T...>) {return std::array<std::size_t, sizeof...(T)>{{ sizeof(T)... }};
}(types{});

4、执行make_integer_sequence

#include <utility>
#include <vector>template<typename T>
consteval std::meta::info make_integer_seq_refl(T N) {std::vector args{^T};for (T k = 0; k < N; ++k) {args.push_back(std::meta::reflect_value(k));}return substitute(^std::integer_sequence, args);
}template<typename T, T N>using make_integer_sequence = [:make_integer_seq_refl<T>(N):];

5、反射枚举

//正向
template <typename E>requires std::is_enum_v<E>
constexpr std::string enum_to_string(E value) {template for (constexpr auto e : std::meta::members_of(^E)) {if (value == [:e:]) {return std::string(std::meta::name_of(e));}}return "<unnamed>";
}enum Color { red, green, blue };
static_assert(enum_to_string(Color::red) == "red");
static_assert(enum_to_string(Color(42)) == "<unnamed>");
//反向
template <typename E>requires std::is_enum_v<E>
constexpr std::optional<E> string_to_enum(std::string_view name) {template for (constexpr auto e : std::meta::members_of(^E)) {if (name == std::meta::name_of(e)) {return [:e:];}}return std::nullopt;
}

6、结构体数组的反射

struct point {float x;float y;float z;
};using points = struct_of_arrays<point, 30>;
// equivalent to:
// struct points {
//   std::array<float, 30> x;
//   std::array<float, 30> y;
//   std::array<float, 30> z;
// };

其它还有几种,在这里不再一一列举。

四、总结

看了c++26中的提案,可以发现其实很多现在的反射框架的轮子基本都可以废弃了。不过,能把反射最终支持,估计得2028年左右了,时间还有的是。喜欢造轮子的同学可以继续造,反正在造轮子的过程中,可以更好的学习反射的原理和实践,同样能更好的理解新标准中的反射。这样,在未来的应用中,也会更加轻松的引入标准库内的反射。
在后面反射实现分析讲解中,基本也会沿着大致相同的路线进行。

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

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

相关文章

迭代器的分类

迭代器的分类&#xff1a; 这里的前置后置递增是a和a&#xff1b; 这里的前值后置递减是a--和--a&#xff1b; 各迭代器的继承关系&#xff1a; 当使用双向迭代器时&#xff0c;可以使用随机迭代器&#xff1b;

THEMIS---Beta Sprint Summary Essay Blog

Which course does this assignment belong to2301-MUSE社区-CSDN社区云What are the requirements for this assignmentbeta SprintThe goal of this assignmentTo summarize the beta task progress and the teams sprintsTeam NameThemisTop-of-the-line collection of essa…

架构师和软件架构

架构师和软件架构 引言 在今天的数字化世界中&#xff0c;软件已成为我们生活和工作的不可或缺的一部分。从简单的应用程序到复杂的系统&#xff0c;软件的设计和开发过程都需要精心的规划和管理。这里&#xff0c;软件架构的概念和软件架构师的角色就显得尤为重要。 软件架…

maui中实现加载更多 RefreshView跟ListView 跳转到详情页 传参(3)

效果如图 这里的很多数据是通过传参过来的的。 代码 例表页加入跳转功能&#xff1a; <ListView ItemsSource"{Binding Items}" ItemAppearing"OnItemAppearing" ItemTapped"OnItemTapped" RowHeight"70" Margin"20"…

visual studio 2019 移除/卸载项目已经如何再加载项目

文章目录 移除解决方案下的某个项目添加已移除的项目移除项目加载已卸载的项目注意事项 移除解决方案下的某个项目 在项目名称上&#xff0c;点击鼠标右键&#xff0c;弹出右键工具栏&#xff0c;找到 移除 功能。 然后鼠标左键点击 移除。 弹出的模态框&#xff0c;选择确定…

代码随想录刷题题Day15

刷题的第十五天&#xff0c;希望自己能够不断坚持下去&#xff0c;迎来蜕变。&#x1f600;&#x1f600;&#x1f600; 刷题语言&#xff1a;C Day15 任务 ● 513.找树左下角的值 ● 112. 路径总和 113.路径总和ii ● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历…

MYSQL备份和恢复

数据库的备份和恢复&#xff1a; 备份&#xff1a;完全备份 增量备份 完全备份&#xff1a;将整个数据库完整的进行备份 增量备份&#xff1a;在完全备份的基础之上&#xff0c;对后续新增的内容进行备份 备份的需求&#xff1a; 在生产环境中&#xff0c;数据的安全至关重…

Docker安装Redis哨兵

目录 Redis哨兵 一、哨兵模式的主要概念和组件 二、哨兵模式的工作流程 三、哨兵配置流程 1、创建Redis哨兵配置文件 2、启动哨兵 3、命令解读 4、 查看哨兵是否正常启动 5、测试主机宕机 四、哨兵运行流程 五、哨兵选举算法 六、哨兵使用建议 Redis哨兵 Redis哨兵…

josef约瑟 时间继电器 DS-23/C AC220V 10S柜内板前接线

系列型号&#xff1a; DS-21时间继电器 &#xff1b;DS-22时间继电器&#xff1b; DS-23时间继电器&#xff1b;DS-24时间继电器&#xff1b; DS-21C时间继电器&#xff1b;DS-22C时间继电器&#xff1b; DS-23C时间继电器&#xff1b; DS-25时间继电器&#xff1b;DS-26…

python/c++ Leetcode题解——746. 使用最小花费爬楼梯

目录 方法一&#xff1a;动态规划 复杂度分析 方法一&#xff1a;动态规划 假设数组 cost 的长度为 n&#xff0c;则 n 个阶梯分别对应下标 0 到 n−1&#xff0c;楼层顶部对应下标 n&#xff0c;问题等价于计算达到下标 n 的最小花费。可以通过动态规划求解。 创建长度为 n…

springboot 学习网站

Spring Boot 系列教程https://www.docs4dev.com/ Spring Boot 教程汇总 http://www.springboot.wiki/ Spring Cloud 微服务教程 http://www.springboot.wiki/ 1、自定义banner   https://www.cnblogs.com/cc11001100/p/7456145.html 2、事件和监听器   https://blog.csd…

孩子都能学会的FPGA:第三十三课——用FPGA实现一个通用的SPI主机接收模块

&#xff08;原创声明&#xff1a;该文是作者的原创&#xff0c;面向对象是FPGA入门者&#xff0c;后续会有进阶的高级教程。宗旨是让每个想做FPGA的人轻松入门&#xff0c;作者不光让大家知其然&#xff0c;还要让大家知其所以然&#xff01;每个工程作者都搭建了全自动化的仿…

如何从 iPhone 上恢复已删除的照片教程分享

您是否错误地删除了 iPhone 上的错误照片&#xff1f;或者您可能已将手机恢复出厂设置&#xff0c;但现在所有照片都消失了&#xff1f;如果您现在遇到这样的情况&#xff0c;我们可以为您提供解决方案。 在本文中&#xff0c;我们将向您展示七种数据恢复方法&#xff0c;可以…

人工智能驱动化学品创新设计的实践与展望

改进化学品研发模式&#xff0c;缩短化学品从发现到应用的时间是化工行业中所有科学研究者和产业人员的最终目 标。本文提出&#xff1a;化学品设计是一个涉及多组分、多尺度和多物理场的复杂过程&#xff0c;现有的实验研究模式难以深入高 效地揭示相关的物理化学机制&#xf…

STM8L151C8单片机学习例程(9)——Unique-ID

直接点击打不开&#xff0c;右键新建窗口打开链接 STM8L151C8单片机学习例程&#xff08;7&#xff09;——Unique-ID

论文解读 | NeurIPS2023:「解释一切」图像概念解释器

点击蓝字 关注我们 AI TIME欢迎每一位AI爱好者的加入&#xff01; 讲者简介 孙奥&#xff1a; 香港科技大学软件安全实验室在读博士&#xff0c;研究兴趣为可解释性人工智能和可信机器学习&#xff0c;主要是从Post-hoc&#xff0c;逻辑和概念的角度分析神经网络的机理 Title 「…

服务器安全的威胁和防范

由于服务器发挥着至关重要的作用&#xff0c;因此存储在服务器上的机密数据和信息非常具有价值。做好服务器安全至关重要。 常见的服务器安全隐患包括&#xff1a; 1.恶意的攻击&#xff1a;遭受CC攻击和DDoS攻击&#xff0c;导致游戏或是网站打不开&#xff0c;严重影响业务…

【智能算法】11种混沌映射算法+2种智能算法示范【鲸鱼WOA、灰狼GWO算法】

目录 1 主要内容 2 部分代码 3 程序结果 4 下载链接 1 主要内容 混沌映射算法是我们在智能算法改进中常用到的方法&#xff0c;本程序充分考虑改进算法应用的便捷性&#xff0c;集成了11种混合映射算法&#xff0c;包括Singer、tent、Logistic、Cubic、chebyshev、Piecewise…

学通python

学通python &#xff08;一. 涉及知识点&#xff09; python基础&#xff08;认识python&#xff0c;环境&#xff0c;pycharm&#xff0c;注释&#xff0c;变量&#xff0c;变量类型&#xff0c;输入输出&#xff0c;运算发&#xff09;流程控制结构&#xff08;判断语句&am…

华为面试题,连续出了三年!

写在前面 据说&#xff0c;这是一道被华为 2021、2022 和 2023 都出过的题目 &#x1f923; 华为是「卷」的发明者&#xff0c;但不是「内卷」发明者&#xff0c;毕竟只有华为是实打实的给加班费。 这么卷的公司&#xff0c;怎么也不更新一下题库。 难道没人做出来就不用考虑换…