e7b09f09dd5c0d09751a4146f24c035329b587a0
[platform/upstream/boost.git] / libs / container / example / doc_move_containers.cpp
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
17 //Non-copyable class
18 class non_copyable
19 {
20    BOOST_MOVABLE_BUT_NOT_COPYABLE(non_copyable)
21
22    public:
23    non_copyable(){}
24    non_copyable(BOOST_RV_REF(non_copyable)) {}
25    non_copyable& operator=(BOOST_RV_REF(non_copyable)) { return *this; }
26 };
27
28 int main ()
29 {
30    using namespace boost::container;
31
32    //Store non-copyable objects in a vector
33    vector<non_copyable> v;
34    non_copyable nc;
35    v.push_back(boost::move(nc));
36    assert(v.size() == 1);
37
38    //Reserve no longer needs copy-constructible
39    v.reserve(100);
40    assert(v.capacity() >= 100);
41
42    //This resize overload only needs movable and default constructible
43    v.resize(200);
44    assert(v.size() == 200);
45
46    //Containers are also movable
47    vector<non_copyable> v_other(boost::move(v));
48    assert(v_other.size() == 200);
49    assert(v.empty());
50
51    return 0;
52 }
53 //]
54 #include <boost/container/detail/config_end.hpp>