From 4122e722a4b43cfcd8a2b29b3cc02209564b2fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Wed, 3 Aug 2016 00:18:11 +0200 Subject: [PATCH] Added constant `static_vector<>/small_vector::static_capacity` to use the configured capacity in constant expressions. --- doc/container.qbk | 6 ++++-- include/boost/container/small_vector.hpp | 5 ++++- include/boost/container/static_vector.hpp | 3 +++ test/small_vector_test.cpp | 2 ++ test/static_vector_test.cpp | 3 +++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/doc/container.qbk b/doc/container.qbk index e0f86f7..bd83a1a 100644 --- a/doc/container.qbk +++ b/doc/container.qbk @@ -1220,8 +1220,10 @@ use [*Boost.Container]? There are several reasons for that: * [@https://svn.boost.org/trac/boost/ticket/12177 Trac #12177: ['"vector::priv_merge uses unqualified uintptr_t"]]. * [@https://svn.boost.org/trac/boost/ticket/12183 Trac #12183: ['"GCC 6.1 thinks boost::container::string violates strict aliasing"]]. * [@https://svn.boost.org/trac/boost/ticket/12286 Trac #12286: ['"PMR flat_map from Boost Container does not compile"]]. - * [@https://svn.boost.org/trac/boost/ticket/12296 Trac #12296: ['"boost::container::{deque,string} combine for a memory leak"]]. - * [@https://svn.boost.org/trac/boost/ticket/12319 Trac #12319: ['"boost::container::flat_set` should be nothrow move constructible"]]. + * [@https://svn.boost.org/trac/boost/ticket/12296 Trac #12296: ['"{deque,string} combine for a memory leak"]]. + * [@https://svn.boost.org/trac/boost/ticket/12319 Trac #12319: ['"flat_set` should be nothrow move constructible"]]. + * [@https://svn.boost.org/trac/boost/ticket/12273 Trac #12273: ['"static_vector max_size() and capacity() should be constant expressions"]]. + Added constant `static_vector<>::static_capacity` to use the configured capacity in constant expressions. * Revised noexcept expressions of default and move constructors in all containers. diff --git a/include/boost/container/small_vector.hpp b/include/boost/container/small_vector.hpp index de3564a..e23b54f 100644 --- a/include/boost/container/small_vector.hpp +++ b/include/boost/container/small_vector.hpp @@ -502,9 +502,12 @@ class small_vector : public small_vector_base #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED + //! @brief The capacity/max size of the container + static const size_type static_capacity = N; + public: BOOST_CONTAINER_FORCEINLINE small_vector() - BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible::value) + BOOST_NOEXCEPT_IF(container_detail::is_nothrow_default_constructible::value) : base_type(initial_capacity_t(), internal_capacity()) {} diff --git a/include/boost/container/static_vector.hpp b/include/boost/container/static_vector.hpp index 82f83c4..ea6b030 100644 --- a/include/boost/container/static_vector.hpp +++ b/include/boost/container/static_vector.hpp @@ -139,6 +139,9 @@ public: //! @brief The const reverse iterator. typedef typename base_t::const_reverse_iterator const_reverse_iterator; + //! @brief The capacity/max size of the container + static const size_type static_capacity = Capacity; + //! @brief Constructs an empty static_vector. //! //! @par Throws diff --git a/test/small_vector_test.cpp b/test/small_vector_test.cpp index 5c1fc3b..adf5f65 100644 --- a/test/small_vector_test.cpp +++ b/test/small_vector_test.cpp @@ -65,6 +65,7 @@ bool test_small_vector_base_test() typedef boost::container::small_vector_base smb_t; { typedef boost::container::small_vector sm5_t; + BOOST_STATIC_ASSERT(sm5_t::static_capacity == 5); sm5_t sm5; smb_t &smb = sm5; smb.push_back(1); @@ -75,6 +76,7 @@ bool test_small_vector_base_test() } { typedef boost::container::small_vector sm7_t; + BOOST_STATIC_ASSERT(sm7_t::static_capacity == 7); sm7_t sm7; smb_t &smb = sm7; smb.push_back(2); diff --git a/test/static_vector_test.cpp b/test/static_vector_test.cpp index 3077866..df03834 100644 --- a/test/static_vector_test.cpp +++ b/test/static_vector_test.cpp @@ -33,6 +33,7 @@ template void test_ctor_ndc() { static_vector s; + BOOST_STATIC_ASSERT((static_vector::static_capacity) == N); BOOST_TEST_EQ(s.size() , 0u); BOOST_TEST(s.capacity() == N); BOOST_TEST(s.max_size() == N); @@ -43,6 +44,7 @@ template void test_ctor_nc(size_t n) { static_vector s(n); + BOOST_STATIC_ASSERT((static_vector::static_capacity) == N); BOOST_TEST(s.size() == n); BOOST_TEST(s.capacity() == N); BOOST_TEST(s.max_size() == N); @@ -64,6 +66,7 @@ template void test_ctor_nd(size_t n, T const& v) { static_vector s(n, v); + BOOST_STATIC_ASSERT((static_vector::static_capacity) == N); BOOST_TEST(s.size() == n); BOOST_TEST(s.capacity() == N); BOOST_TEST_THROWS( s.at(n), std::out_of_range );