美文网首页
true_type and false_type

true_type and false_type

作者: 404Not_Found | 来源:发表于2022-02-21 05:58 被阅读0次
    • 作者: 雪山肥鱼
    • 时间:20220217 07:51
    • 目的: true_type and false_type
    # ture_type 简介
    # true_type 与 true 的区别
    

    true_type 简介

    #include <iostream>
    /*
    //两个类型(类模板)
    很明显true_type false_type 是类模板被实例化了的类型
        using true_type = integer_constant<bool, true>;
        using false_type = integer_constant<bool, false>;
    */
    
    int main(int argc, char **argv) {
        return 0;
    }
    

    true_type 与true的区别

    true_type :代表类型(类类型)
    ture:代表的是值

    true_type func(); //可以
    true func();//不可以
    

    注意与bool的区别:
    bool 可代表 true false
    true_type 只能代表 ture

    template<bool var>
    struct BoolConstant {
        using type = BoolConstant;//写成 using type = BoolConstant<val>也行
        static constexpr bool value = val;
    };
    
    using TrueType = BoolConstant<true>;
    using FalseType = BoolConstant<false>;
    
    int main(int argc, char **argv) {
        return 0;
    }
    
    1. TrueType(std::true_type) 是一个被实例化的类模板,即一个类 类型。代表true的含义。
    2. 一般是当基类被继承,子类也就具备了真或假的意味。
      比如 is_pointer, is_union,is_function等类模板都是继承BoolConstant<true>
    is_union<int>::value
    
    1. 可以当作一种返回类型被使用
      后续萃取使用
    FalseType myfunc1();//返回真
    TrueType myfunc2();// 返回假
    

    举例:

    using TrueType = BoolConstant<true>;
    using FalseType = BoolConstant<false>;
    
    template <typename T, bool val>
    struct AClass {
        AClass() {
            cout << "AClass:AClass()执行了" << endl;
            if (val) {
                T tmpa = 15;
            }
            else {
                T tmpa = "abc"
            }
        }
    };
    
    int main(int argc, char **argv) {
        AClass<int, true> tmpobj1;
        //AClass<string, false> tmpobj2;
        return 0;
    }
    

    上述代码看上去没有问题,实则不能通过编译。
    如果是int,int tmpa = "abc",是明显有问题的。

    1>c:\users\liush\desktop\template\ture_typeandfalse_type\main.cpp(29): error C2440: “初始化”: 无法从“const char [4]”转换为“T”
    1>        with
    1>        [
    1>            T=int
    1>        ]
    

    解决方案1:
    用 if constexpr

    template <typename T, bool val>
    struct AClass {
        AClass() {
            cout << "AClass:AClass()执行了" << endl;
            if  constexpr (val) {
                T tmpa = 15;
            }
            else {
                T tmpa = "abc";
            }
        }
    };
    
    int main(int argc, char **argv) {
        AClass<int, true> tmpobj1;
        //AClass<string, false> tmpobj2;
        return 0;
    }
    

    解决方案2:truetype falsetype 简单应用

    template<bool var>
    struct BoolConstant {
        using type = BoolConstant;//写成 using type = BoolConstant<val>也行
        static constexpr bool value = var;
    };
    
    using TrueType = BoolConstant<true>;
    using FalseType = BoolConstant<false>;
    
    template <typename T, bool val>
    struct AClass {
        AClass() {
            cout << "AClass:AClass()执行了" << endl;
            /*
            if  constexpr (val) {
                T tmpa = 15;
            }
            else {
                T tmpa = "abc";
            }
            */
    
            //AClassEx 是成员函数
            AClassEx(BoolConstant<val>());//BoolConstant<val>() 创建临时对象
    
        }
    
        void AClassEx(TrueType abc) {
            cout << "AClassEx(TrueType abc)" << endl;
            T tmpa = 15;
        }
    
        void AClassEx(FalseType abc) {
            cout << "AClassEx(FalseType abc)" << endl;
            T tmpa = "abc";
        }
    
    };
    
    int main(int argc, char **argv) {
        AClass<int, true> tmpobj1;
        AClass<string, false> tmpobj2;
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:true_type and false_type

          本文链接:https://www.haomeiwen.com/subject/lbvelrtx.html