news 2026/7/15 15:40:51

CANN/asc-devkit enable_if类型特性

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
CANN/asc-devkit enable_if类型特性

enable_if

产品支持情况

产品

是否支持

Ascend 950PR/Ascend 950DT

Atlas A3 训练系列产品/Atlas A3 推理系列产品

Atlas A2 训练系列产品/Atlas A2 推理系列产品

Atlas 200I/500 A2 推理产品

x

Atlas 推理系列产品AI Core

x

Atlas 推理系列产品Vector Core

x

Atlas 训练系列产品

x

功能说明

enable_if是定义于<type_traits>头文件的一个模板元编程工具,它能够在程序编译时根据某个条件启用或禁用特定的函数模板、类模板或模板特化,以此实现更精细的模板重载和类型选择,增强代码的灵活性和安全性。

enable_if是一个模板结构体,有两个模板参数:模板参数Bp是一个布尔值,表示条件;模板参数Tp是一个类型,默认值为void。当Bp为false时,enable_if没有嵌套的type成员。当Bp为true时,enable_if有一个嵌套的type成员,其类型为Tp。

函数原型

template <bool Bp, typename Tp> struct enable_if;

参数说明

表 1模板参数说明

参数名

含义

Bp

布尔值,表示条件。

Tp

类型,默认值为void。

约束说明

返回值说明

enable_if的静态常量成员type用于获取返回值,enable_if<Bp, Tp>::type取值如下:

  • Tp:Bp为true。
  • void:Bp为false。

调用示例

template <typename T> class Calculator { public: // 当 T 是整数类型时启用此成员函数 template <typename U = T> typename AscendC::Std::enable_if<AscendC::Std::is_integral<U>::value, U>::type __aicore__ inline multiply(U a, U b) { AscendC::PRINTF("Integral type multiplication"); return a * b; } // 当 T 不是整数类型时启用此成员函数 template <typename U = T> typename AscendC::Std::enable_if<!AscendC::Std::is_integral<U>::value, U>::type __aicore__ inline multiply(U a, U b) { AscendC::PRINTF("Non-integral type multiplication"); return a * b; } }; // 通用模板类 template <typename T, typename Enable = void> class Container { public: __aicore__ inline Container() { AscendC::PRINTF("Generic container.\n"); } }; // 特化版本,当 T 是整数类型时启用 template <typename T> class Container<T, typename AscendC::Std::enable_if<AscendC::Std::is_integral<T>::value>::type> { public: __aicore__ inline Container() { AscendC::PRINTF("Integral container.\n"); } }; // 当 T 是整数类型时启用该函数 template <typename T> __aicore__ inline typename AscendC::Std::enable_if<AscendC::Std::is_integral<T>::value, T>::type add(T a, T b) { AscendC::PRINTF("Integral type addition."); return a + b; } // 当 T 不是整数类型时启用该函数 template <typename T> __aicore__ inline typename AscendC::Std::enable_if<!AscendC::Std::is_integral<T>::value, T>::type add(T a, T b) { AscendC::PRINTF("Non-integral type addition."); return a + (-b); } Calculator<int> intCalculator; int intResult = intCalculator.multiply((int)2, (int)3); AscendC::PRINTF("Result of integral multiplication: %d\n", intResult); Calculator<float> doubleCalculator; float doubleResult = doubleCalculator.multiply((float)2.5, (float)3.5); AscendC::PRINTF("Result of non-integral multiplication: %f\n", doubleResult); Container<float> genericContainer; Container<int> integralContainer; intResult = add(1, 2); AscendC::PRINTF("Integer result: %d\n", intResult); doubleResult = add((float)1.5, (float)2.5); AscendC::PRINTF("float result: %f\n", doubleResult);
// 执行结果: Integral type multiplicationResult of integral multiplication: 6 Non-integral type multiplicationResult of non-integral multiplication: 8.750000 Generic container. Integral container. Integral type addition.Integer result: 3 Non-integral type addition.float result: -1.000000

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/15 15:40:37

掌握无损音频压缩艺术:FLAC 1.5.0完全指南与实战技巧

掌握无损音频压缩艺术&#xff1a;FLAC 1.5.0完全指南与实战技巧 【免费下载链接】flac Free Lossless Audio Codec 项目地址: https://gitcode.com/gh_mirrors/fl/flac 在数字音频存储领域&#xff0c;FLAC&#xff08;Free Lossless Audio Codec&#xff09;作为开源无…

作者头像 李华
网站建设 2026/7/15 15:40:01

2026年AI Agent岗位激增300%!高薪抢夺浪潮背后,它将如何重塑产业?

打开招聘软件搜索 “AI Agent”&#xff0c;扑面而来的海量招聘岗位直观印证当下行业热潮。2026 年人工智能赛道招聘重心全面转移&#xff0c;从过去单纯的大模型微调、NLP 算法岗&#xff0c;转向 AI 智能体全链路开发人才&#xff0c;招聘规模、薪资水平双双领跑技术类岗位。…

作者头像 李华
网站建设 2026/7/15 15:38:48

CANN/asc-devkit浮点数转换API

__bfloat162half_rna 产品支持情况产品 是否支持 Ascend 950PR/Ascend 950DT √ Atlas A3 训练系列产品/Atlas A3 推理系列产品 x Atlas A2 训练系列产品/Atlas A2 推理系列产品 x功能说明 将bfloat16类型数据转换为向远离零方向舍入的half类型数据。 函数原型 __simt_callee__…

作者头像 李华
网站建设 2026/7/15 15:38:40

Study Experience (暑假篇 1)

作为一个计算机专业的学生 偶然听到暑期留校的老师提及该应用可以记录自己的一些学习经历 于是我决定创建一个属于自己的计算机学习经历 这应该比手抄记录 或者单独记录在备忘录要方便灵活的多 这里竟然还可以插入代码 真是个不错的应用 而且用户很多 有很多高手 值得我去学习提…

作者头像 李华
网站建设 2026/7/15 15:37:57

5分钟掌握Notepad++ Markdown插件:实时预览与高效编辑终极指南

5分钟掌握Notepad Markdown插件&#xff1a;实时预览与高效编辑终极指南 【免费下载链接】MarkdownViewerPlusPlus A Notepad Plugin to view a Markdown file rendered on-the-fly 项目地址: https://gitcode.com/gh_mirrors/ma/MarkdownViewerPlusPlus 你是否厌倦了在…

作者头像 李华