conditional_t和enable_if_t的实现

conditional,enable,if,实现 · 浏览次数 : 9

小编点评

**Conditional_t** ```cpp template using conditional_t = typename __conditional<_Cond, _Tp, _Else>::type; ``` **Enable_if_t** ```cpp template using enable_if_t = typename enable_if<_Cond, _Tp>::type; ``` **区别** * **conditional_t** 是一个模板函数,可以用于检查各种类型并根据条件指定类型。 * **enable_if_t** 是一个模板类,可以用于检查条件并根据条件定义类型。 **用途** * **conditional_t** 可以用于创建根据条件创建类型的新类型。 * **enable_if_t** 可以用于创建根据条件定义类型的新类,并在该类中定义特定的类型。

正文

  conditional_t和enable_if_t是元编程里面很相似却有有着一定区别的模板。形如conditional_t<_Cond, _If, _Else>是指如果_Cond表达式为true,则类型为_If,否则类型为_Else。而形如enable_if_t<_Cond, _Tp>是指如果_Cond表达式为true,则类型为_Tp,否则不定义类型。

 

conditional_t的标准实现

template<bool>
    struct __conditional
    {
      template<typename _Tp, typename>
	using type = _Tp;
    };

  template<>
    struct __conditional<false>
    {
      template<typename, typename _Up>
	using type = _Up;
    };

  // More efficient version of std::conditional_t for internal use (and C++11)
  template<bool _Cond, typename _If, typename _Else>
    using __conditional_t
      = typename __conditional<_Cond>::template type<_If, _Else>;

这里指出一点,最后一行::后面的template其实可以去掉,变成::type<_If, _Else>。

 

enable_if_t的实现

template<bool, typename _Tp = void>
    struct enable_if
    { };

  // Partial specialization for true.
  template<typename _Tp>
    struct enable_if<true, _Tp>
    { typedef _Tp type; };

  // __enable_if_t (std::enable_if_t for C++11)
  template<bool _Cond, typename _Tp = void>
    using __enable_if_t = typename enable_if<_Cond, _Tp>::type;

对true类型进行了特化,只有true的时候才定义类型,否则不定义任何类型。_Tp的默认缺省类型是void。

 

  关于enable_if_t的简单应用,在之前的C++系列里面也有提到过。

与conditional_t和enable_if_t的实现相似的内容:

conditional_t和enable_if_t的实现

conditional_t和enable_if_t是元编程里面很相似却有有着一定区别的模板。形如conditional_t<_Cond, _If, _Else>是指如果_Cond表达式为true,则类型为_If,否则类型为_Else。而形如enable_if_t<_Cond, _Tp>是指如果_Co

掌握Spring条件装配的秘密武器

本文详细解释了如何使用Spring的@Profile和@Conditional注解实现条件装配,通过具体的示例可以更好地理解这两个注解的用法和适用场景。

salesforce零基础学习(一百三十七)零碎知识点小总结(九)

本篇参考: https://help.salesforce.com/s/articleView?id=release-notes.rn_lab_conditional_visibiliy_tab.htm&release=250&type=5 https://help.salesforce.com/s

知识点笔记(java / 数据库)

1-java stream peek vs map;; 2-java各种map;; 3-mysql列变行;; 4-java @Conditional;; 5-redis命名空间;; 等等等等...

详解AQS中的condition源码原理

摘要:condition用于显式的等待通知,等待过程可以挂起并释放锁,唤醒后重新拿到锁。 本文分享自华为云社区《AQS中的condition源码原理详细分析》,作者:breakDawn。 condition的用法 condition用于显式的等待通知,等待过程可以挂起并释放锁,唤醒后重新拿到锁。 和

三星电视无法下载《条款和条件、隐私政策》的问题 (消息代码: 0-1)

因为想看一部影片引发的对三星电视 app 功能的探索,没有重刷固件、没有远程协助,解决问题的方案居然是再简单不过的手机热点,将解决过程记录下来希望能帮到更多的三星中国用户,从一个侧面见证了三星产品在中国大陆退潮的缩影…

【Azure 存储服务】访问Azure Blob File遇见400-Condition Headers not support错误的解决之路

This XML file does not appear to have any style information associated with it. The document tree is shown below.

分布式任务调度内的 MySQL 分页查询优化

本文主要通过图示介绍了用主键进行分片查询的过程,介绍了主键分页查询存在SQL性能问题,如何去创建高效的索引去优化主键分页查询的SQL性能问题。对于数据分布不均如何发现,提供了一些SQL查询案例来进行参考,对MySQL Index Condition Pushdown优化算法做了一些简单介绍。

MySql索引下推知识分享

作者:刘邓忠 Mysql 是大家最常用的数据库,下面为大家带来 mysql 索引下推知识点的分享,以便巩固 mysql 基础知识,如有错误,还请各位大佬们指正。 1 什么是索引下推 索引下推 (Index Condition Pushdown,索引条件下推,简称 ICP),是 MySQL5.6 版本