• std::true_type和std::false_type


    一、认识std::true_type和std::false_type

    std::true_type和std::false_type实际上是类型别名,源码如下:

    template <class _Ty,
        _Ty _Val>
    struct integral_constant { // convenient template for integral constant types
        static constexpr _Ty value = _Val;
    
        using value_type = _Ty;
        using type       = integral_constant;
    
        constexpr operator value_type() const noexcept { // return stored value
            return value;
        }
    
        _NODISCARD constexpr value_type operator()() const noexcept { // return stored value
            return value;
        }
    };
    
    // ALIAS TEMPLATE bool_constant
    template <bool _Val>
    using bool_constant = integral_constant<bool, _Val>;
    
    using true_type  = bool_constant<true>;
    using false_type = bool_constant<false>;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    通过源码我们可以知道,其实它就是一个结构体模板
    下面看看它的简单使用:

    int main()
    {	
    
    	std::true_type a;
    	std::false_type b;
    	cout <<a  << endl;
    	cout <<b<< endl;
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    结果:
    在这里插入图片描述

    二、与true和false的区别

    1.true_type、false_type代表的是类型(类类型),而true、false代表的是值,不要弄混。
    2.类型有类型用到的场合,值有值能用到的场合,比如函数返回类型,用true_type表示返回一个true类型的值,用false_type表示返回一个false类型的值。有的同学,可能会疑惑,因为用bool类型也能达到同样的效果。这其实是错误的认识,因为bool既能代表true也能代表false,而true_type类型代表的就是true,false_type类型代表的就是false.

    三、为什么要使用std::true_type和std::false_type

    先记住下面几点
    (1)TrueType和FalseType代表一个类类型,TrueType(std::true_type)代表一种true(真)的含义,而False_Type(std::false_type)代表一种false(假)的含义。
    (2)一般是当作基类被继承。当作为基类被继承时,派生类也就具备了真或假的这种意味。
    (3)可以当作一种返回类型被使用,比如:

    FalseType myfunc1(); //返回“假”这种含义
    TrueType myfunc2(); //返回“真”这种含义
    
    • 1
    • 2

    有如下的例子:

    template<typename T,bool vaxl>
    struct AClass
    {
    
    	AClass()
    	{
    		if (vaxl)
    		{
    			T a = 15;
    
    		}
    		else
    		{
    			T a = "abc";
    		}
    		
    	}
    	
    };
    
    int main()
    {	
    	AClass<int, true> obj1;
    	AClass<string, false> obj2;
    	system("pause");
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    结果:
    在这里插入图片描述
    上面的代码,当第二个参数为true时,T为int,false时,T为string类型,按道理说应该能编译过啊,那这里为什么编译不过呢?其实这是编译器的一些考量,编译器能够在编译的时候判断出执行AClass类模板构造函数走哪个分支,但是从编译出代码的角度来讲,不管时if条件分支,还是else条件分支,编译器都会去编译,所以编译器编译到T a = “abc”;代码的时会报错。这里可以使用编译期间if语句(if constexpr)来解决,如下:

    template<typename T,bool vaxl>
    struct AClass
    {
    	AClass()
    	{
    		if  constexpr(vaxl)
    		{
    			T a = 15;
    		}
    		else
    		{
    			T a = "abc";
    		}		
    	}
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    结果:
    在这里插入图片描述
    接下来使用true_type,false_type解决

    template<typename T,bool vaxl>
    struct AClass
    {
    	AClass()
    	{
    		fun(bool_constant<vaxl>());
    	}
    
    	void fun(std::true_type)
    	{
    		T a = 15;
    	}
    
    	void fun(std::false_type)
    	{
    		T a = "abc";
    	}
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    这个例子时一个针对std::true_type,std::false_type类型的简单应用,后面再萃取技术会继续讲解。

  • 相关阅读:
    aarch64 arm64 部署 stable diffusion webui 笔记 【2】继续安装其他依赖 gfpgan
    算法进阶-2sat-cf-1400C
    (附源码)ssm微课堂知识考核系统 毕业设计 141147
    dependent '..\fjsp\customtableview.h' does not exist.
    WebDAV之葫芦儿·派盘+账本(简洁记账)
    echart3D地图
    Java扫码点餐小程序源码 智慧点餐系统源码 点餐APP SaaS模式
    美联储历次加息周期及结果
    Android自定义View之条件筛选菜单
    Qt 各种数据类型
  • 原文地址:https://blog.csdn.net/FairLikeSnow/article/details/125629425