duplicate.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*!
  2. @file
  3. Defines `boost::hana::duplicate`.
  4. Copyright Louis Dionne 2013-2022
  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_DUPLICATE_HPP
  9. #define BOOST_HANA_DUPLICATE_HPP
  10. #include <boost/hana/fwd/duplicate.hpp>
  11. #include <boost/hana/concept/comonad.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/extend.hpp>
  15. #include <boost/hana/functional/id.hpp>
  16. namespace boost { namespace hana {
  17. //! @cond
  18. template <typename W_>
  19. constexpr decltype(auto) duplicate_t::operator()(W_&& w) const {
  20. using W = typename hana::tag_of<W_>::type;
  21. using Duplicate = BOOST_HANA_DISPATCH_IF(duplicate_impl<W>,
  22. hana::Comonad<W>::value
  23. );
  24. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  25. static_assert(hana::Comonad<W>::value,
  26. "hana::duplicate(w) requires 'w' to be a Comonad");
  27. #endif
  28. return Duplicate::apply(static_cast<W_&&>(w));
  29. }
  30. //! @endcond
  31. template <typename W, bool condition>
  32. struct duplicate_impl<W, when<condition>> : default_ {
  33. template <typename X>
  34. static constexpr decltype(auto) apply(X&& x)
  35. { return hana::extend(static_cast<X&&>(x), hana::id); }
  36. };
  37. }} // end namespace boost::hana
  38. #endif // !BOOST_HANA_DUPLICATE_HPP