Difference between revisions of "cpp/experimental/is detected"
(merge detected_or) |
(tweak) |
||
Line 19: | Line 19: | ||
{{dcl end}} | {{dcl end}} | ||
− | The alias template {{tt|detected_or}} is an alias for an unspecified class type with two public member typedefs {{tt|value_t}} and {{tt|type}}, | + | The alias template {{tt|detected_or}} is an alias for an unspecified class type with two public member typedefs {{tt|value_t}} and {{tt|type}}, which are defined as follows: |
* If the ''template-id'' {{tt|Op<Args...>}} is valid and denotes a type, then {{tt|value_t}} is an alias for {{lc|std::true_type}}, and {{tt|type}} is an alias for {{tt|Op<Args...>}}; | * If the ''template-id'' {{tt|Op<Args...>}} is valid and denotes a type, then {{tt|value_t}} is an alias for {{lc|std::true_type}}, and {{tt|type}} is an alias for {{tt|Op<Args...>}}; |
Revision as of 13:04, 23 May 2015
Defined in header <experimental/type_traits>
|
||
template< template<class...> class Op, class... Args > using is_detected = /* see below */; |
(library fundamentals TS v2) | |
template< template<class...> class Op, class... Args > using detected_t = /* see below */; |
(library fundamentals TS v2) | |
template< class Default, template<class...> class Op, class... Args > using detected_or = /* see below */; |
(library fundamentals TS v2) | |
The alias template detected_or
is an alias for an unspecified class type with two public member typedefs value_t
and type
, which are defined as follows:
- If the template-id
Op<Args...>
is valid and denotes a type, thenvalue_t
is an alias for std::true_type, andtype
is an alias forOp<Args...>
; - Otherwise,
value_t
is an alias for std::false_type andtype
is an alias forDefault
.
The alias template is_detected
is equivalent to typename detected_or<std::experimental::nonesuch, Op, Args...>::value_t. It is an alias for std::true_type if the template-id Op<Args...> is valid and denotes a type; otherwise it is an alias for std::false_type.
The alias template detected_t
is equivalent to typename detected_or<std::experimental::nonesuch, Op, Args...>::type. It is an alias for Op<Args...> if that template-id is valid and denotes a type; otherwise it is an alias for the class std::experimental::nonesuch.
Additional utilities
template< template<class...> class Op, class... Args > constexpr bool is_detected_v = is_detected<Op, Args...>::value; |
(library fundamentals TS v2) | |
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 = 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 To, template<class...> class Op, class... Args> using is_detected_convertible = 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) | |
The alias template is_detected_exact
checks whether detected_t<Op, Args...> is Expected
.
The alias template is_detected_convertible
checks whether detected_t<Op, Args...> is convertible to To
.
Example
#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; }; int main() { 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::is_detected_exact_v<Meow&, copy_assign_t, Meow>::value, "Copy assignment of Meow should return Meow&!"); }
#include <experimental/type_traits> #include <cstddef> 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 Meow { using difference_type = int; }; struct Purr {}; int main() { static_assert(std::is_same<difference_type<Meow>, int>::value, "Meow's difference_type should be int!"); static_assert(std::is_same<difference_type<Purr>, std::ptrdiff_t>::value, "Purr's difference_type should be ptrdiff_t!"); }