doc_move_containers.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2009-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/container/detail/config_begin.hpp>
  11. #include <boost/container/detail/workaround.hpp>
  12. //[doc_move_containers
  13. #include <boost/container/vector.hpp>
  14. #include <boost/move/utility_core.hpp>
  15. #include <cassert>
  16. //Non-copyable class
  17. class non_copyable
  18. {
  19. BOOST_MOVABLE_BUT_NOT_COPYABLE(non_copyable)
  20. public:
  21. non_copyable(){}
  22. non_copyable(BOOST_RV_REF(non_copyable)) {}
  23. non_copyable& operator=(BOOST_RV_REF(non_copyable)) { return *this; }
  24. };
  25. int main ()
  26. {
  27. using namespace boost::container;
  28. //Store non-copyable objects in a vector
  29. vector<non_copyable> v;
  30. non_copyable nc;
  31. v.push_back(boost::move(nc));
  32. assert(v.size() == 1);
  33. //Reserve no longer needs copy-constructible
  34. v.reserve(100);
  35. assert(v.capacity() >= 100);
  36. //This resize overload only needs movable and default constructible
  37. v.resize(200);
  38. assert(v.size() == 200);
  39. //Containers are also movable
  40. vector<non_copyable> v_other(boost::move(v));
  41. assert(v_other.size() == 200);
  42. assert(v.empty());
  43. return 0;
  44. }
  45. //]
  46. #include <boost/container/detail/config_end.hpp>