fork download
  1. #include <type_traits>
  2. #include <algorithm>
  3. #include <cassert>
  4. #include <iostream>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. #define FORWARD(arg) static_cast<decltype(arg)&&>(arg)
  9.  
  10. template <class T>
  11. typename std::conditional
  12. <
  13. std::is_same<typename std::decay<T>::type&&, T&&>{},
  14. T&&,
  15. typename std::decay<T>::type
  16. >::type make_copy(T&& t)
  17. {
  18. using underlying_type = typename std::remove_reference<T>::type;
  19. static_assert(
  20. !std::is_array<underlying_type>{} && !std::is_function<underlying_type>{},
  21. "the argument does not designate a non-array object");
  22. return FORWARD(t);
  23. }
  24.  
  25. template <class Stream, class Container>
  26. void print_sorted(Stream &stream, Container&& container)
  27. {
  28. auto&& sorted_container = ::make_copy(FORWARD(container));
  29. std::sort(sorted_container.begin(), sorted_container.end());
  30. for (const auto& elem : sorted_container)
  31. stream << elem << '\n';
  32. stream << '\n';
  33. }
  34.  
  35. int main()
  36. {
  37. std::vector<int> v{3, 4, 1, 5, 2};
  38. const std::vector<int> cv = v;
  39.  
  40. ::print_sorted(std::cout, v);
  41. ::print_sorted(std::cout, cv);
  42. ::print_sorted(std::cout, std::move(cv));
  43. assert(v == cv);
  44.  
  45. ::print_sorted(std::cout, std::move(v));
  46. assert(v != cv);
  47. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
1
2
3
4
5

1
2
3
4
5

1
2
3
4
5

1
2
3
4
5