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

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

posted @ 2021-05-18 14:21  jernia  阅读(263)  评论(0编辑  收藏  举报