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

泛型编程技巧——使用std::enable_if实现按类型进行条件编译​

std::enable_if可以以多种形式使用,包括:

  • 作为类模板或函数模板参数
  • 作为返回类型(不适用于构造函数和析构函数)
  • 作为附加函数参数(不适用于运算符重载)

std::enable_if can be used in many forms, including:

  • as an additional function argument (not applicable to most operator overloads),
  • as a return type (not applicable to constructors and destructors),
  • as a class template or function template parameter.

std::enable_if 核心是结合 SFINAE(Substitution Failure Is Not An Error)机制,实现编译期的条件分支逻辑。

c++14中,定义了using enable_if_t = typename enable_if<B,T>::type;

1. 作为类模板或函数模板参数

1.1 控制类模板的参数类型

例子,

// the partial specialization of A is enabled via a template parameter
template<class T, class Enable = void>
class A {}; // primary templatetemplate<class T>
class A<T, typename std::enable_if<std::is_floating_point<T>::value>::type>
{}; // specialization for floating point typesint main()
{A<int>{}; // OK: matches the primary templateA<double>{}; // OK: matches the partial specialization
}

另一个例子,

// 基础模板
template <typename Mat, typename Vec, typename Enable = void>
class Derived : public Base<Mat, Vec> {};  // 模版特化,针对double类型
template <typename Mat, typename Vec>
class Derived<Mat, Vec, std::enable_if_t<std::is_same_v<typename Mat::ElementType, std::complex<double>>>> : public Base<Mat, Vec> {};// 模版特化,针对std::complex<double>类型
template <typename Mat, typename Vec>
class Derived<Mat, Vec, std::enable_if_t<std::is_same_v<typename Mat::ElementType, std::complex<double>>>> : public Base<Mat, Vec> {};

在这个例子中,Derived继承自Base基类,但是能够针对Mat元素类型来进行特化。

这个例子使用了c++14新特性enable_if_t 。

1.2 控制函数模板的参数类型

例如,根据类型是否为整数类型来选择不同的函数实现:

#include <type_traits>
#include <iostream>template<typename T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
void print(T value) {std::cout << "Integral value: " << value << std::endl;
}template<typename T, typename std::enable_if<!std::is_integral<T>::value, T>::type* = nullptr>
void print(T value) {std::cout << "Non-integral value: " << value << std::endl;
}int main() {print(5);       // Integral value: 5print(3.14);    // Non-integral value: 3.14return 0;
}

在这个例子中:

  • 第一个 print 函数模板仅对整数类型有效(std::is_integral<T>::valuetrue)。

  • 第二个 print 函数模板对非整数类型有效(std::is_integral<T>::valuefalse)。

2. 作为返回类型

例子,

#include <type_traits>
#include <iostream>template <typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type
foo(T x)
{std::cout << "foo int";return x * 2;
}template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type
foo(T x)
{std::cout << "foo float";return x / 2.0;
}

在这个示例中,我们定义了两个函数模板foo,它们根据不同的类型来返回不同的结果。第一个foo函数只能接受整型参数,并返回这个整数的两倍。第二个foo函数只能接受浮点型参数,并返回这个浮点数的一半。

我们运行

foo(2);
foo(2.1);

将得到打印结果

foo int
foo float

3. 作为附加函数参数

以下是一个使用 std::enable_if 作为附加函数参数的示例,假设我们希望根据传入的类型是否为指针类型来选择不同的函数实现:

#include <iostream>
#include <type_traits>// 重载函数1:仅对指针类型有效
template<typename T>
void print(T* value, typename std::enable_if<std::is_pointer<T*>::value>::type* = nullptr) {std::cout << "Pointer value: " << *value << std::endl;
}// 重载函数2:对非指针类型有效
template<typename T>
void print(T value, typename std::enable_if<!std::is_pointer<T>::value>::type* = nullptr) {std::cout << "Non-pointer value: " << value << std::endl;
}int main() {int a = 10;int* ptr = &a;print(ptr);  // 调用指针版本的 printprint(a);    // 调用非指针版本的 printreturn 0;
}

运行程序后,输出结果如下:

Pointer value: 10
Non-pointer value: 10

参考:

https://en.cppreference.com/w/cpp/types/enable_if.html

C++ std::enable_if的简明指南_c enable if-CSDN博客

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

相关文章:

  • 测试面试题总结一
  • 7.Demo Js执行同步任务,微任务,宏任务的顺序(3)
  • QGIS新手教程3:QGIS矢量图层导入与导出+Shape Tools插件绘图
  • C++查找算法全解析:从基础到高级应用
  • MQTT协议:物联网时代的通信基石
  • 手写 vue 源码 === runtime-dom 实现
  • Solidity 开发指南:构建你的第一个 DApp
  • JMeter函数整理
  • 软件安全:漏洞利用与渗透测试剖析、流程、方法、案例
  • 光量子计算芯片改变了黄仁勋成见?英伟达拟与PsiQuantum联手颠覆未来算力
  • 运维实施42-SHELL 编程
  • 【envoy】-1.安装与下载源码
  • 渗透实战PortSwigger靶场:lab13存储型DOM XSS详解
  • 期末复习(学习)之机器学习入门基础
  • SPI通信协议(软件SPI读取W25Q64)
  • 本地部署网站流量分析工具 Matomo 并实现外部访问
  • 机器学习实战37-基于情感字典和机器学习的股市舆情分析可视化系统
  • Python-面向对象
  • 敏捷开发中如何避免过度加班
  • 银河麒麟V10ServerSP3中快速安装Minio及注册自启服务
  • 中小制造企业转型:低成本国产工业软件替代方案实践
  • 国标GB28181视频平台EasyGBS视频实时监控系统打造换热站全景可视化管理方案
  • 04.两数之和
  • 基于STM32F407的情绪感知智能助眠系统
  • 8天Python从入门到精通【itheima】-68(元组)
  • 数据“出国”需办“签证”: 如何申请数据出境安全评估?
  • 《校园生活平台从 0 到 1 的搭建》第一篇:创建项目与构建目录结构
  • 数据库表中「不是 null」的含义
  • Cursor 工具项目构建指南: Python 3.8 环境下的 Prompt Rules 约束
  • 项目实战——C语言扫雷游戏