在seastar
的shard.hh
有这么一段代码。
template <typename T>
std::enable_if_t<std::is_base_of<peering_sharded_service<T>, T>::value>
set_container(T& service) {
service.set_container(this);
}
template <typename T>
std::enable_if_t<!std::is_base_of<peering_sharded_service<T>, T>::value>
set_container(T& service) {
}
里面使用到C++11里的两个新特性:std::enable_if
和std::is_base_of
1 std::enable_if
查看c++的type_traits
源码,std::enable_if
的实现如下:
// Primary template.
/// Define a member typedef @c type only if a boolean constant is true.
template<bool, typename _Tp = void>
struct enable_if
{ };
// Partial specialization for true. 模板特化
template<typename _Tp>
struct enable_if<true, _Tp>
{ typedef _Tp type; };
网友评论