embedding.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/core/to.hpp>
  6. #include <boost/hana/core/when.hpp>
  7. #include <vector>
  8. namespace hana = boost::hana;
  9. namespace boost { namespace hana {
  10. template <typename To, typename From>
  11. struct to_impl<std::vector<To>, std::vector<From>,
  12. when<is_convertible<From, To>::value>>
  13. : embedding<is_embedded<From, To>::value>
  14. {
  15. static std::vector<To> apply(std::vector<From> const& xs) {
  16. std::vector<To> result;
  17. for (auto const& x: xs)
  18. result.push_back(to<To>(x));
  19. return result;
  20. }
  21. };
  22. }}
  23. int main() {
  24. BOOST_HANA_RUNTIME_CHECK(
  25. hana::to<std::vector<int>>(std::vector<float>{1.1, 2.2, 3.3})
  26. ==
  27. std::vector<int>{1, 2, 3}
  28. );
  29. static_assert(!hana::is_embedded<std::vector<float>, std::vector<int>>{}, "");
  30. }