std::experimental::is_detected, std::experimental::detected_t, std::experimental::detected_or

来自cppreference.com
定义于头文件 <experimental/type_traits>
template< template<class...> class Op, class... Args >
using is_detected = /* see below */;
(库基础 TS v2)
template< template<class...> class Op, class... Args >
using detected_t = /* see below */;
(库基础 TS v2)
template< class Default, template<class...> class Op, class... Args >
using detected_or = /* see below */;
(库基础 TS v2)

别名模板 detected_or 是拥有二个公开成员 typedef value_ttype 的未指定类类型的别名,它们定义如下:

  • 模板 id Op<Args...> 合法,则 value_tstd::true_type 的别名,而 typeOp<Args...> 的别名;
  • 否则, value_tstd::false_type 的别名而 typeDefault 的别名。


别名模板 is_detected 等价于 typename detected_or<std::experimental::nonesuch, Op, Args...>::value_t 。若模板 id Op<Args...> 合法,则它是 std::true_type 的别名;否则它是 std::false_type 是别名。


别名模版 detected_t 等价于 typename detected_or<std::experimental::nonesuch, Op, Args...>::type 。若模板 id Op<Args...> 合法则它是 Op<Args...> 的别名;否则它是类 std::experimental::nonesuch 的别名。

目录

[编辑] 附加工具

template< template<class...> class Op, class... Args >
constexpr bool is_detected_v = is_detected<Op, Args...>::value;
(库基础 TS v2)
template< class Default, template<class...> class Op, class... Args >
using detected_or_t = typename detected_or<Default, Op, Args...>::type;
(库基础 TS v2)
template <class Expected, template<class...> class Op, class... Args>
using is_detected_exact = is_same<Expected, detected_t<Op, Args...>>;
(库基础 TS v2)
template <class Expected, template<class...> class Op, class... Args>

constexpr bool is_detected_exact_v =

    is_detected_exact<Expected, Op, Args...>::value;
(库基础 TS v2)
template <class To, template<class...> class Op, class... Args>
using is_detected_convertible = is_convertible<detected_t<Op, Args...>, To>;
(库基础 TS v2)
template <class To, template<class...> class Op, class... Args>

constexpr bool is_detected_convertible_v =

    is_detected_convertible<To, Op, Args...>::value;
(库基础 TS v2)

别名模版 is_detected_exact 检查 detected_t<Op, Args...> 是否为 Expected

别名模板 is_detected_convertible 检查 detected_t<Op, Args...> 是否可转换为 To

[编辑] 可能的实现

namespace detail {
template <class Default, class AlwaysVoid,
          template<class...> class Op, class... Args>
struct detector {
  using value_t = std::false_type;
  using type = Default;
};
 
template <class Default, template<class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...> {
  // 注意 std::void_t 是 C++17 特性
  using value_t = std::true_type;
  using type = Op<Args...>;
};
 
} // namespace detail
 
template <template<class...> class Op, class... Args>
using is_detected = typename detail::detector<nonesuch, void, Op, Args...>::value_t;
 
template <template<class...> class Op, class... Args>
using detected_t = typename detail::detector<nonesuch, void, Op, Args...>::type;
 
template <class Default, template<class...> class Op, class... Args>
using detected_or = detail::detector<Default, void, Op, Args...>;

[编辑] 示例

#include <experimental/type_traits>
#include <cstddef>
 
template<class T>
using copy_assign_t = decltype(std::declval<T&>() = std::declval<const T&>());
 
struct Meow { };
struct Purr { void operator=(const Purr&) = delete; };
 
static_assert(std::experimental::is_detected<copy_assign_t, Meow>::value,
              "Meow should be copy assignable!");
static_assert(!std::experimental::is_detected_v<copy_assign_t, Purr>,
              "Purr should not be copy assignable!");
static_assert(std::experimental::is_detected_exact_v<Meow&, copy_assign_t, Meow>,
              "Copy assignment of Meow should return Meow&!");
 
template<class T>
using diff_t = typename T::difference_type;
 
template <class Ptr>
using difference_type = std::experimental::detected_or_t<std::ptrdiff_t, diff_t, Ptr>;
 
struct Woof { using difference_type = int; };
struct Bark {};
 
static_assert(std::is_same<difference_type<Woof>, int>::value,
              "Woof's difference_type should be int!");
static_assert(std::is_same<difference_type<Bark>, std::ptrdiff_t>::value,
              "Bark's difference_type should be ptrdiff_t!");
 
int main() {}


[编辑] 参阅