std::experimental:: is_detected, std::experimental:: detected_t, std::experimental:: detected_or
|
ヘッダーで定義
<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
は、以下のように定義される2つの公開メンバー型定義
value_t
および
type
を持つ、未指定のクラス型のエイリアスです:
-
テンプレートID
Op
<
Args...
>
が有効な型を表す場合、
value_tは std::true_type のエイリアスであり、typeは Op < Args... > のエイリアスである。 -
それ以外の場合、
value_tは std::false_type のエイリアスであり、typeはDefaultのエイリアスである。
エイリアステンプレート
is_detected
は
typename
detected_or
<
std::
experimental
::
nonesuch
, Op, Args...
>
::
value_t
と等価です。これは
std::true_type
のエイリアスとなります(
template-id
Op
<
Args...
>
が有効な型を表す場合)。それ以外の場合は
std::false_type
のエイリアスとなります。
エイリアステンプレート
detected_t
は
typename
detected_or
<
std::
experimental
::
nonesuch
, Op, Args...
>
::
type
と等価です。これは
Op
<
Args...
>
が有効な型を表す場合にはそのエイリアスであり、そうでない場合にはクラス
std::experimental::nonesuch
のエイリアスとなります。
追加ユーティリティ
template< template<class...> class Op, class... Args >
constexpr bool is_detected_v = is_detected<Op, Args...>::value;
|
(library fundamentals TS v2) | |
template< template<class...> class Op, class... Args >
constexpr inline bool is_detected_v = is_detected<Op, Args...>::value;
|
(library fundamentals TS v3) | |
template< class Default, template<class...> class Op, class... Args >
using detected_or_t = typename detected_or<Default, Op, Args...>::type;
|
(library fundamentals TS v2) | |
template< class Expected, template<class...> class Op, class... Args >
using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
|
(library fundamentals TS v2) | |
template< class Expected, template<class...> class Op, class... Args >
constexpr bool is_detected_exact_v =
is_detected_exact<Expected, Op, Args...>::value;
|
(library fundamentals TS v2) | |
template< class Expected, template<class...> class Op, class... Args >
constexpr inline bool is_detected_exact_v =
is_detected_exact<Expected, Op, Args...>::value;
|
(library fundamentals TS v3) | |
template< class To, template<class...> class Op, class... Args >
using is_detected_convertible =
std::is_convertible<detected_t<Op, Args...>, To>;
|
(library fundamentals TS v2) | |
template< class To, template<class...> class Op, class... Args >
constexpr bool is_detected_convertible_v =
is_detected_convertible<To, Op, Args...>::value;
|
(library fundamentals TS v2) | |
template< class To, template<class...> class Op, class... Args >
constexpr inline bool is_detected_convertible_v =
is_detected_convertible<To, Op, Args...>::value;
|
(library fundamentals TS v3) | |
エイリアステンプレート 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...> { 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 <cstddef> #include <experimental/type_traits> 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() {}