zoukankan      html  css  js  c++  java
  • 类模板偏特化时的默认参数

    起因:在使用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. 偏特化模板中不能有默认参数(待确定)

  • 相关阅读:
    day66
    1
    day65
    BeautifulSoup
    day60
    day59
    day49
    day48
    [S5PV210] PWM
    [S5PV210] Clock
  • 原文地址:https://www.cnblogs.com/chenia/p/14780753.html
Copyright © 2011-2022 走看看