• 类模板偏特化时的默认参数


    起因:在使用enable_if时,发现,enable_if<std::is_xxx<T>::value>::type,并没有传递第二个参数,那么第二个参数怎么确定为void类型。

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

    template <typename T>
    struct enable_if<true, T>{
    using type = T;
    };
     
    实验:偏特化版本typename T会遵循 typename T=void:
     
    template <typename U, typename T = void> 
    class test {
    public:
    test() {
    std::cout << "test1" << std::endl;
    }

    void aaa(){
    std::cout << "aaa test1" << std::endl;
    }
    };


    template <typename T>
    class test<int, T> {
    public:
    test() {
    std::cout << "test2" << std::endl;
    std::cout<< std::is_void<T>::value << std::endl;
    std::cout << typeid(T).name() << std::endl;
    }
    void aaa(){
    std::cout << "aaa test2" << std::endl;
    }
    };
     
    test<int> t2;

    后续:

        发现类似问题:https://stackoverflow.com/questions/18700558/default-template-parameter-partial-specialization

        1. 尽可能优先匹配偏特化版本,

        2. 偏特化模板中不能有默认参数(待确定)

  • 相关阅读:
    字符编码及文件处理
    列表、元祖、字典及集合的内置方法
    数字类型、字符串及列表的内置方法
    流程控制(if while for)
    一些基本概念及数据类型
    编程语言的发展及变量
    python 入门基本知识
    叁拾贰(转)
    叁拾壹
    叁拾
  • 原文地址:https://www.cnblogs.com/chenia/p/14780753.html
Copyright © 2020-2023  润新知