seastar中std::enable_if使用

2019-06-15  本文已影响0人  dnsir

seastarshard.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_ifstd::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; };
上一篇 下一篇

猜你喜欢

热点阅读