Micro template library A library for building device drivers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

89 lines
3.0 KiB

  1. /*!
  2. * \file use.h
  3. * \brief Template meta-programming SFINAE helpers
  4. *
  5. * Copyright (C) 2018 Christos Choutouridis
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef __utl_meta_use_h__
  21. #define __utl_meta_use_h__
  22. #include <utl/impl/impl.h>
  23. #include <utl/meta/logical.h>
  24. /*!
  25. * \ingroup meta
  26. * \defgroup use
  27. * conditional use support header. This is a SFINAE helper
  28. */
  29. //! @{
  30. namespace utl {
  31. /*!
  32. * void_t
  33. * Utility meta-function that maps a sequence of any types to the type void
  34. * \note The idea is:
  35. * template <typename ...>
  36. * using void_t = void;
  37. *
  38. * Until CWG 1558 (a C++14 defect), unused parameters in alias templates were not
  39. * guaranteed to ensure SFINAE and could be ignored, so earlier compilers require
  40. * a more complex definition of void_t, such as the following implementation
  41. * https://en.cppreference.com
  42. */
  43. //! @{
  44. template<typename... _Ts>
  45. struct void_t_impl {
  46. typedef void type;
  47. };
  48. //! The actual void_t type alias
  49. template<typename... _Ts>
  50. using void_t = typename void_t_impl<_Ts...>::type;
  51. //! @}
  52. //! Alias template for if_
  53. template<bool _C, typename _Tp = void>
  54. using if_t = typename if_<_C, _Tp>::type;
  55. //! Publicly recognized alias template for if_
  56. template<bool _C, typename _Tp = void>
  57. using enable_if_t = typename if_<_C, _Tp>::type;
  58. //! Uniform alias template for if_
  59. template<bool _C, typename _Tp = void>
  60. using use_if_t = typename if_<_C, _Tp>::type;
  61. //! If same type resolves to _Ret, else SFINAE
  62. template <typename _T1, typename _T2, typename _Ret =_T1>
  63. using use_if_same_t = typename if_<same_<_T1, _T2>::value, _Ret>::type;
  64. //! If not same type resolves to _Ret, else SFINAE
  65. template <typename _T1, typename _T2, typename _Ret =_T1>
  66. using use_if_not_same_t = typename if_<!same_<_T1, _T2>::value, _Ret>::type;
  67. //! If any type (_T1 or _T2) type resolves to _Ret, else to SFINAE
  68. template <typename _T1, typename _T2, typename _Ret =_T1>
  69. using use_if_any_t = typename if_<or_<_T1, _T2>::value, _Ret>::type;
  70. //! If both type (_T1 and _T2) type resolves to _Ret, else to SFINAE
  71. template <typename _T1, typename _T2, typename _Ret =_T1>
  72. using use_if_both_t = typename if_<and_<_T1, _T2>::value, _Ret>::type;
  73. }
  74. //! @}
  75. #endif /* __utl_meta_use_h__ */