|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- /*!
- * \file detection.h
- * \brief Detection idiom based on WG21's N4502 from Walter E. Brown
- */
- #ifndef __utl_meta_detection_h__
- #define __utl_meta_detection_h__
-
- #include <utl/core/impl.h>
- #include <utl/meta/basic.h>
- #include <type_traits>
-
- /*!
- * \ingroup meta
- * \defgroup detection Detection
- * Detection idiom support header.
- */
- //! @{
-
- namespace utl {
- namespace meta {
-
- /*!
- * \name void_t implementation
- */
- //! @{
- #if defined(UTL_WORKAROUND_CWG_1558)
- template<typename... _Ts>
- struct void_ {
- using type = void;
- };
- //! void_t type alias
- template<typename... _Ts>
- using void_t = eval<void_<_Ts...>>;
- #else
- //! void_ meta-function that maps a sequence of any types to the type void
- template <typename...> using void_ = void;
- //! void_t meta-function that maps a sequence of any types to the type void
- template <typename...> using void_t = void;
- #endif
- //! @}
-
- /*!
- * \brief
- * Not a type to use in detected idiom.
- *
- * This type can not be constructed, destructed or copied.
- */
- struct nat_ {
- nat_() = delete;
- ~nat_() = delete;
- nat_(nat_ const&) = delete;
- void operator = (nat_ const&) = delete;
- };
-
- //! \name Detector for detection idiom
- //! @{
- namespace details {
- template <typename Default,
- typename AlwaysVoid,
- template<typename...> class Op, typename... Args>
- struct detector {
- using detected = false_;
- using type = Default;
- };
-
- template <typename Default,
- template<typename...> class Op, typename... Args>
- struct detector <Default, void_t<Op<Args...>>, Op, Args...> {
- using detected = true_;
- using type = Op<Args...>;
- };
-
- //! helper for detected_or_t
- template <typename Default,
- template<typename...> class Op, typename... Args>
- using detected_or = detector<Default, void, Op, Args...>;
- } // namespace details
- //! @}
-
- /*!
- * \name detection interface
- */
- //! @{
-
- /*!
- * Checks if Op<Args...> is a valid expression without evaluating it.
- *
- * \tparam Op a meta-callback function to pass Args...
- * \tparam Args... types to pass to Op for checking
- * \return status of the operation [bool_]
- * \arg true_ if Op<Args...> is valid expression
- * \arg false_ if Op<Args...> is not valid
- *
- * \code
- * // archetypal alias for a copy assignment operation
- * template< class T > using copy_assign_t = decltype( declval<T&>() = declval<T const &>() );
- *
- * template< class T > using is_copy_assignable = is_detected< copy_assign_t, T >;
- * \endcode
- */
- template <template<typename...> class Op, typename... Args>
- using is_detected = typename details::detector<nat_, void, Op, Args...>::detected;
-
- //! Detection predicate
- template< template<typename...> class Op, typename... Args>
- constexpr bool is_detected_v = is_detected<Op, Args...>::value;
-
- /*!
- * Detection tool that evaluates to Op<Args...> if it's valid and to nat_ if not
- *
- * \tparam Op metafunction detector
- * \tparam Args... The arguments to pass to \p Op and check if is well formed
- * \return The result type
- * \arg Op<Args...> if is well formed
- * \arg nat_ if Op<Args...> is ill formed
- *
- * \code
- * template <typename T> using try_type = typename T::type; // detector
- * template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
- * static_assert( std::is_same<nat_, detected_t<try_type, A<int>> >(), ""); // detection failed
- * static_assert( std::is_same<A<int>&, detected_t<try_ppT, A<int>> >(), ""); // detection succeed
- *
- * // if mFun<int, int> is well formed
- * static_assert(std::is_same< mFun<int, int>, detected_t<mFun, int, int> >(), "");
- * \endcode
- */
- template <template<typename...> class Op, typename... Args>
- using detected_t = eval <
- details::detector<nat_, void, Op, Args...>
- >;
-
- /*!
- * Detection tool that evaluates to Op<Args...> if it's valid and to \p Default if not
- *
- * \tparam Default The resulting type if detection fail
- * \tparam Op metafunction detector
- * \tparam Args... The arguments to pass to \p Op and check if is well formed
- * \return The result type
- * \arg Op<Args...> if is well formed
- * \arg Default if Op<Args...> is ill formed
- *
- * \code
- * template <typename T> using try_type = typename T::type; // detector
- * template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
- * static_assert( std::is_same<Foo, detected_or_t<Foo, try_type, A<int>> >(), ""); // detection failed
- * static_assert( std::is_same<A<int>&, detected_or_t<Foo, try_ppT, A<int>> >(), ""); // detection succeed
- *
- * // if mFun<int, int> is well formed
- * static_assert(std::is_same< mFun<int, int>, detected_or_t<void, mFun, int, int> >(), "");
- * \endcode
- */
- template <typename Default,
- template<typename...> class Op, typename... Args>
- using detected_or_t = eval <
- details::detected_or<Default, Op, Args...>
- >;
-
- /*!
- * Detection tool that evaluates to true_ if evaluation of Op<Args...>
- * is \p Expected and to false_ if not
- *
- * \tparam Expected The expected resulting type if detection succeed
- * \tparam Op metafunction detector
- * \tparam Args... The arguments to pass to \p Op and check if is well formed
- * \return The result type
- * \arg true_ if Op<Args...> is well formed and evaluate to Expected
- * \arg false_ Any other case
- *
- * \code
- * template <typename T> using try_type = typename T::type; // detector
- * template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
- * static_assert( std::is_same<false_, is_detected_exact<int, try_type, A<int>> >(), ""); // detection failed
- * static_assert( std::is_same<true_, is_detected_exact<A<int>&, try_ppT, A<int>> >(), ""); // detection succeed
- *
- * // if mFun<int, int> is well formed
- * static_assert(std::is_same< true_, is_detected_exact<mFun<int, int>, mFun, int, int> >(), "");
- * \endcode
- */
- template <typename Expected,
- template<typename...> class Op, typename... Args >
- using is_detected_exact = eval <
- same<Expected, detected_t<Op, Args...>>
- >;
-
- //! evaluates to true if evaluation of Op<Args...> is \p Expected and to false if not
- template <typename Expected,
- template<typename...> class Op, typename... Args >
- constexpr bool is_detected_exact_v = is_detected_exact< Expected, Op, Args...>::value;
-
- /*!
- * Detection tool that evaluates to true_ if evaluation of Op<Args...> is convertible
- * to \p To and to false_ if not
- *
- * \tparam To The to convert to if detection succeed
- * \tparam Op metafunction detector
- * \tparam Args... The arguments to pass to \p Op and check if is well formed
- * \return The result type
- * \arg true_ if Op<Args...> is well formed and convertible to To
- * \arg false_ Any other case
- *
- * \code
- * template <typename T> using try_type = typename T::type; // detector
- * template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
- * static_assert( std::is_same<false_, is_detected_convertible<Foo, try_type, A<int>> >(), ""); // detection failed
- * static_assert( std::is_same<true_, is_detected_convertible<A<int>&&, try_ppT, A<int>> >(), "");// detection succeed
- *
- * // if mFun<int, int> is well formed but not convertible to Foo
- * static_assert(std::is_same< false_, is_detected_convertible<Foo, mFun, int, int> >(), "");
- * \endcode
- */
- template <typename To,
- template<typename...> class Op, typename... Args >
- using is_detected_convertible = eval <
- std::is_convertible< detected_t<Op, Args...>, To >
- >;
-
- //! evaluates to true if evaluation of Op<Args...> is convertible to \p To
- //! and to false if not
- template <typename To,
- template<typename...> class Op, typename... Args >
- constexpr bool is_detected_convertible_v =
- is_detected_convertible<To, Op, Args...>::value;
- //! @}
-
- }}
- //!@}
-
- #endif /* __utl_meta_detection_h__ */
|