Micro template library A library for building device drivers
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

82 řádky
2.1 KiB

  1. /*!
  2. * \file sfinae.h
  3. * \brief Template meta-programming SFINAE helpers
  4. */
  5. #ifndef __utl_meta_sfinae_h__
  6. #define __utl_meta_sfinae_h__
  7. #include <utl/core/impl.h>
  8. #include <type_traits>
  9. /*!
  10. * \ingroup meta
  11. * \defgroup sfinae SFINAE
  12. * conditional use support header.
  13. */
  14. //! @{
  15. namespace utl {
  16. namespace meta {
  17. //! \name enable_if from STL
  18. //! @{
  19. //! enable_if, imported from stl
  20. template <bool If, typename _Tp = void> using enable_if = std::enable_if<If, _Tp>;
  21. //! alias template for enable_if
  22. template<bool If, typename _Tp = void> using enable_if_t = eval< enable_if<If, _Tp> >;
  23. //! @}
  24. //! \name when implementation
  25. //! @{
  26. namespace details {
  27. // template <typename... T>
  28. // struct dev_null { using type = dev_null; }; //< Same as typelist
  29. template <bool If> struct when_ { };
  30. template <> struct when_<true> { using type = void; };
  31. }
  32. //! Tool to enable a partial specialization only if a boolean condition is true.
  33. //! Well formed only if \p If is true
  34. template <bool If>
  35. using when = eval< details::when_<If> >;
  36. // //! Well formed only if all of \p Ifs are \c true
  37. // template <bool ...Ifs>
  38. // using when_all = details::dev_null<
  39. // when<Ifs>...
  40. // >;
  41. //! @}
  42. //! If same type resolves to _Ret, else SFINAE
  43. template <typename _T1, typename _T2, typename _Ret =_T1>
  44. using use_if_same_t = enable_if_t<
  45. same_<_T1, _T2>::value, _Ret
  46. >;
  47. //! If not same type resolves to _Ret, else SFINAE
  48. template <typename _T1, typename _T2, typename _Ret =_T1>
  49. using use_if_not_same_t = enable_if_t<
  50. !same_<_T1, _T2>::value, _Ret
  51. >;
  52. //! If any type (_T1 or _T2) type resolves to _Ret, else to SFINAE
  53. template <typename T1, typename... Ts>
  54. using use_if_any_t = enable_if_t<
  55. or_<T1, Ts...>::value, T1
  56. >;
  57. //! If both type (_T1 and _T2) type resolves to _Ret, else to SFINAE
  58. template <typename T1, typename... Ts>
  59. using use_if_all_t = enable_if_t<
  60. and_<T1, Ts...>::value, T1
  61. >;
  62. }}
  63. //! @}
  64. #endif /* __utl_meta_sfinae_h__ */