move.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. #include <cstring>
  12. #include <string>
  13. #include <vector>
  14. #include <range/v3/core.hpp>
  15. #include <range/v3/view/move.hpp>
  16. #include <range/v3/algorithm/copy.hpp>
  17. #include <range/v3/iterator/operations.hpp>
  18. #include <range/v3/utility/copy.hpp>
  19. #include "../simple_test.hpp"
  20. #include "../test_utils.hpp"
  21. int main()
  22. {
  23. using namespace ranges;
  24. static const char * const data[] = {"'allo", "'allo", "???"};
  25. std::vector<MoveOnlyString> vs(begin(data), end(data));
  26. auto x = vs | views::move;
  27. {
  28. CPP_assert(common_range<decltype(x)>);
  29. CPP_assert(sized_range<decltype(x)>);
  30. CPP_assert(view_<decltype(x)>);
  31. CPP_assert(common_range<decltype(x)>);
  32. CPP_assert(sized_range<decltype(x)>);
  33. CPP_assert(random_access_iterator<decltype(x.begin())>);
  34. using I = decltype(x.begin());
  35. CPP_assert(same_as<iterator_tag_of<I>, std::random_access_iterator_tag>);
  36. CPP_assert(same_as<
  37. typename std::iterator_traits<I>::iterator_category,
  38. std::random_access_iterator_tag>);
  39. CHECK(bool(*x.begin() == "'allo"));
  40. }
  41. {
  42. std::vector<MoveOnlyString> vs2(x.begin(), x.end());
  43. static_assert(std::is_same<MoveOnlyString&&, decltype(*x.begin())>::value, "");
  44. ::check_equal(vs2, {"'allo", "'allo", "???"});
  45. ::check_equal(vs, {"", "", ""});
  46. }
  47. {
  48. MoveOnlyString rgs[] = {"can", "you", "hear", "me", "now?"};
  49. auto rng = debug_input_view<MoveOnlyString>{rgs} | views::move;
  50. MoveOnlyString target[sizeof(rgs) / sizeof(rgs[0])];
  51. copy(rng, target);
  52. ::check_equal(rgs, {"", "", "", "", ""});
  53. ::check_equal(target, {"can", "you", "hear", "me", "now?"});
  54. }
  55. return test_result();
  56. }