1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2013-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/core/no_exceptions_support.hpp> 11 //[doc_custom_vector 12 #include <boost/container/vector.hpp> 13 #include <boost/static_assert.hpp> 14 #include <boost/core/no_exceptions_support.hpp> 15 16 //Make sure assertions are active 17 #ifdef NDEBUG 18 #undef NDEBUG 19 #endif 20 #include <cassert> 21 main()22int main () 23 { 24 using namespace boost::container; 25 26 //This option specifies that a vector that will use "unsigned char" as 27 //the type to store capacity or size internally. 28 typedef vector_options< stored_size<unsigned char> >::type size_option_t; 29 30 //Size-optimized vector is smaller than the default one. 31 typedef vector<int, new_allocator<int>, size_option_t > size_optimized_vector_t; 32 BOOST_STATIC_ASSERT(( sizeof(size_optimized_vector_t) < sizeof(vector<int>) )); 33 34 //Requesting capacity for more elements than representable by "unsigned char" 35 //is an error in the size optimized vector. 36 bool exception_thrown = false; 37 /*<-*/ 38 #ifndef BOOST_NO_EXCEPTIONS 39 BOOST_TRY{ size_optimized_vector_t v(256); } BOOST_CATCH(...){ exception_thrown = true; } BOOST_CATCH_END 40 #else 41 exception_thrown = true; 42 #endif //BOOST_NO_EXCEPTIONS 43 /*->*/ 44 //=try { size_optimized_vector_t v(256); } 45 //=catch(...){ exception_thrown = true; } 46 47 assert(exception_thrown == true); 48 49 //This option specifies that a vector will increase its capacity 50% 50 //each time the previous capacity was exhausted. 51 typedef vector_options< growth_factor<growth_factor_50> >::type growth_50_option_t; 52 53 //Fill the vector until full capacity is reached 54 vector<int, new_allocator<int>, growth_50_option_t > growth_50_vector(5, 0); 55 const std::size_t old_cap = growth_50_vector.capacity(); 56 growth_50_vector.resize(old_cap); 57 58 //Now insert an additional item and check the new buffer is 50% bigger 59 growth_50_vector.push_back(1); 60 assert(growth_50_vector.capacity() == old_cap*3/2); 61 62 return 0; 63 } 64 //] 65