首页 > 其他 > 详细

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

时间:2021-05-18 16:01:39      阅读:16      评论:0      收藏:0      [点我收藏+]

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

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

原文:https://www.cnblogs.com/chenia/p/14780753.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!