when.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*!
  2. @file
  3. Forward declares `boost::hana::when` and `boost::hana::when_valid`.
  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_FWD_CORE_WHEN_HPP
  9. #define BOOST_HANA_FWD_CORE_WHEN_HPP
  10. #include <boost/hana/config.hpp>
  11. BOOST_HANA_NAMESPACE_BEGIN
  12. //! @ingroup group-core
  13. //! Enable a partial specialization only if a boolean condition is true.
  14. //!
  15. //! You might also want to take a look at `when_valid`, which provides
  16. //! similar functionality but enables a specialziation only when some
  17. //! expression is well-formed.
  18. //!
  19. //! > #### Rationale for using `when` instead of `std::enable_if`
  20. //! > `when` is used to control the priority of partial specializations
  21. //! > in a finer grained manner than what can be achieved with the usual
  22. //! > `typename Enable = void` and `std::enable_if` pattern. For example,
  23. //! > a partially specialized tag-dispatched method will have a higher
  24. //! > priority than an equivalent specialization that uses `when`. For
  25. //! > more details, see the tutorial section on [tag-dispatching][1].
  26. //!
  27. //!
  28. //! Example
  29. //! -------
  30. //! @include example/core/when.cpp
  31. //!
  32. //! [1]: @ref tutorial-core-tag_dispatching
  33. template <bool condition>
  34. struct when;
  35. namespace core_detail {
  36. template <typename ...>
  37. struct always_true { static constexpr bool value = true; };
  38. }
  39. //! @ingroup group-core
  40. //! Variant of `when` allowing specializations to be enabled only if an
  41. //! expression is well-formed.
  42. //!
  43. //! `when_valid<...>` is always equivalent to `when<true>`. However, when
  44. //! used inside a partial specialization, SFINAE will cause the partial
  45. //! specialization to be ignored when the expression is ill-formed.
  46. //!
  47. //!
  48. //! Example
  49. //! -------
  50. //! @include example/core/when_valid.cpp
  51. //!
  52. //!
  53. //! @bug
  54. //! Using `when_valid` seems to trigger ambiguous partial specializations
  55. //! on GCC.
  56. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  57. template <typename ...>
  58. using when_valid = when<true>;
  59. #else
  60. template <typename ...Dummy>
  61. using when_valid = when<
  62. core_detail::always_true<Dummy...>::value
  63. >;
  64. #endif
  65. BOOST_HANA_NAMESPACE_END
  66. #endif // !BOOST_HANA_FWD_CORE_WHEN_HPP