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