dispatch_if.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*!
  2. @file
  3. Defines `BOOST_HANA_DISPATCH_IF`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_DETAIL_DISPATCH_IF_HPP
  9. #define BOOST_HANA_DETAIL_DISPATCH_IF_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <type_traits>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. struct deleted_implementation {
  14. template <typename ...T>
  15. static constexpr auto apply(T&& ...) = delete;
  16. };
  17. //! @ingroup group-details
  18. //! Dispatch to the given implementation method only when a condition is
  19. //! satisfied.
  20. //!
  21. //! If the condition is satisfied, this macro is equivalent to the type
  22. //! `IMPL`. Otherwise, it is equivalent to a type with a deleted static
  23. //! function named `apply`. When a tag-dispatching error happens, the
  24. //! condition should be false and the deleted static function `apply`
  25. //! will prevent the compiler from generating too much garbage.
  26. //!
  27. //! @note
  28. //! When `BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS` is defined, the
  29. //! condition is always ignored and this macro expands to the
  30. //! implementation only.
  31. //!
  32. //! @remark
  33. //! This must be implemented as a macro, because we don't want the
  34. //! condition to be evaluated at all when
  35. //! `BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS` is defined.
  36. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  37. #define BOOST_HANA_DISPATCH_IF(IMPL, ...) \
  38. ::std::conditional_t< \
  39. (__VA_ARGS__), \
  40. IMPL, \
  41. ::boost::hana::deleted_implementation \
  42. > \
  43. /**/
  44. #else
  45. #define BOOST_HANA_DISPATCH_IF(IMPL, ...) IMPL
  46. #endif
  47. BOOST_HANA_NAMESPACE_END
  48. #endif // !BOOST_HANA_DETAIL_DISPATCH_IF_HPP