当前位置: 首页 > web >正文

一个模板元编程示例

代码

#include <iostream>
#include <type_traits>// 基础模板定义
template <bool AddOrRemoveRef> struct Fun_;// 偏特化:添加左值引用
template <>
struct Fun_<true>{template <typename T>using type = typename std::add_lvalue_reference<T>::type;
};// 偏特化:移除引用
template <>
struct Fun_<false> {template<typename T>using type = typename std::remove_reference<T>::type;
};// 正确的模板别名定义
template <bool AddOrRemove, typename T>
using Fun = typename Fun_<AddOrRemove>::template type<T>;// 正确的Res_定义
template <typename T>
using Res_ = Fun<false, T>;int main() {// 测试Res_<int&> h = 3;  // h的类型是intstd::cout << h << std::endl;return 0;
}

这段代码展示了使用模板元编程技术实现的条件类型转换,根据布尔值参数决定添加左值引用或移除引用。下面逐步解析其工作原理:

1. 基础模板声明

template <bool AddOrRemoveRef> struct Fun_;
  • 声明主模板Fun_,接受一个布尔值非类型参数AddOrRemoveRef

  • 作用:作为分发开关,根据布尔值选择不同特化版本

2. 特化版本实现

(1) true 特化:添加左值引用
template <>
struct Fun_<true> {template <typename T>using type = typename std::add_lvalue_reference<T>::type;
};
  • AddOrRemoveRef=true时激活

  • 使用std::add_lvalue_reference将任意类型T转换为左值引用类型T&

  • 示例:int → int&

(2) false 特化:移除引用
template <>
struct Fun_<false> {template<typename T>using type = typename std::remove_reference<T>::type;
};
  • AddOrRemoveRef=false时激活

  • 使用std::remove_reference移除类型的所有引用修饰

  • 示例:int& → intint&& → int

3. 统一接口封装

template <bool AddOrRemove, typename T>
using Fun = typename Fun_<AddOrRemove>::template type<T>;
  • 创建别名模板Fun简化调用

  • 关键语法

    • typename:指明Fun_<AddOrRemove>::type是依赖类型名

    • template:指明type是依赖模板名

  • 调用方式:Fun<flag, T> 根据flag选择转换操作

4. 特定功能封装

template <typename T>
using Res_ = Fun<false, T>;
  • 固定false参数的便捷别名

  • 专用功能:移除任何传入类型的引用

  • 等价于:Res_<T> = std::remove_reference_t<T>

5. 测试代码分析

Res_<int&> h = 3;  // 等价于 int h = 3
  1. Res_<int&> 实例化过程:

    • 通过Res_调用Fun<false, int&>

    • 激活Fun_<false>::type<int&>

    • 执行std::remove_reference<int&>::type → int

  2. 变量h实际类型为int

  3. 赋值3符合整型要求

设计模式总结

组件作用示例输入→输出
Fun_<true>添加左值引用int → int&
Fun_<false>移除所有引用const int& → const int
Res_专用移除引用工具double&& → double

关键语法解析

using Fun = typename Fun_<B>::template type<T>;
// 1. typename   → 声明后面是类型(依赖名称解析)
// 2. template   → 表明type是模板(防止编译器误解析)
// 3. ::type     → 访问嵌套类型

典型应用场景

  1. 通用类型处理框架中条件转换

  2. 元函数转发时减少::type重复书写

  3. 构建复杂类型运算的中间层

这种设计模式在标准库元编程中常见(如std::conditional的实现),通过布尔开关分发处理逻辑,提高模板代码的复用性和可读性。

http://www.xdnf.cn/news/14006.html

相关文章:

  • 前端三剑客基础案例001
  • 05 - SimAM模块
  • Docker 下备份 Mariadb 数据库文件
  • [技术积累]成熟的前端和后端开发框架
  • 全新RA2L2 MCU超低功耗、支持CAN及USB-C、拓展工业及便携式应用
  • 洛谷 数楼梯 高精度
  • 【SystemVerilog 2023 Std】第5章 词法约定 Lexical conventions (2)
  • opencv 之 视频类 VideoCapture、VideoWriter详细介绍
  • RAD:通过基于大规模 3DGS 的强化学习训练端-到-端驾驶策略
  • 深度解析Linux用户生态:账户架构设计与系统运维实战技巧》
  • 服务器代码知识点补充
  • 【MV】上下文感知选择算法:基于关键词映射简单实现
  • 论文阅读:arxiv 2025 Chain of Draft: Thinking Faster by Writing Less
  • 4步使用 vue3 路由
  • Unity编辑器-获取Projectwindow中拖拽内容的路径
  • 【JVM】- 类加载与字节码结构2
  • Agent 处理流程
  • “在同一事务中“ 的含义
  • 【工具教程】批量PDF识别提取区域的内容重命名,将PDF指定区域位置的内容提取出来改名的具体操作步骤
  • 蘑菇街商品详情接口技术解析
  • Tlias-web 管理系统项目知识点复盘总结
  • 东土科技参与国家重点研发计划 ,共同研发工业智控创新技术
  • Vue里面的映射方法
  • 弹性梁:绘图、分析与可视化-AI云计算数值分析和代码验证
  • linux命令-用户与用户组
  • 什么是redis
  • 【k8s】阿里云ACK服务中GPU实例部署问题
  • QMainWindow、QDialog 和 QWidget区别
  • ubuntu 无法访问位置 error mounting 解决办法 双系统
  • 腐烂之息-(Breath of Decay VR ) 硬核VR游戏