diff --git a/CLAUDE.md b/CLAUDE.md index 5e35a44f..7e97e198 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -62,8 +62,11 @@ build.jam b2 project and dependency list - **Debug mode**: `BOOST_CB_ENABLE_DEBUG=1` makes iterators derive from `debug_iterator_base` and register with the container, so invalidated iterators are caught by `BOOST_CB_ASSERT`. It is off by default. Some - behaviour differs between modes: for example, `constant_erase_test` and - `soft_iterator_invalidation` only make sense with debug off. + behaviour differs between modes. For example, `soft_iterator_invalidation` + only makes sense with debug off, and debug mode overwrites erased memory, so + `constant_erase_test` skips its checks of erased memory when debug is on. + For checks like these, `test.hpp` provides `CB_TEST_DEBUG_ONLY(expr)` and + `CB_TEST_DEBUG_EXCLUDE(expr)`. - **Internal macros**: `BOOST_CB_ASSERT`, `BOOST_CB_IS_CONVERTIBLE` and `BOOST_CB_ASSERT_TEMPLATED_ITERATOR_CONSTRUCTORS` are defined in `circular_buffer.hpp` and `#undef`'d at its end. As a result, the @@ -81,9 +84,16 @@ build.jam b2 project and dependency list - **Exceptions** use `BOOST_TRY` / `BOOST_CATCH(...)` / `BOOST_CATCH_END` / `BOOST_RETHROW` and `boost::throw_exception`. The code must build with `BOOST_NO_EXCEPTIONS`. -- **Scalar fast paths**: `erase_begin(n)`, `erase_end(n)` and `clear()` take - constant time for scalar `T`, because they dispatch on `is_scalar` - and no destructors run. Preserve these complexity +- **Trivially destructible fast paths**: `erase_begin(n)`, `erase_end(n)`, + `clear()` and the destructor take constant time when `T` is trivially + destructible and the allocator doesn't need `destroy()` calls. They dispatch + on the private `trivial_erase` type: `has_trivial_destructor` + and `circular_buffer_trivial_destroy` (in `circular_buffer_fwd.hpp`). + The latter is true only for the default allocator + (`circular_buffer_default_allocator::type`), and users specialize it to + opt in for other allocators. The fast path skips the allocator's `destroy()` + as well as the destructor, so it is opt-in for any non-default allocator. + Debug mode always takes the slow path. Preserve these complexity guarantees, since they are documented. ## Conventions @@ -172,15 +182,13 @@ copy): INC="-Iinclude $(for d in $BOOST/libs/*/include; do case $d in *circular_buffer*) ;; *) printf -- '-isystem %s ' $d;; esac; done)" for std in c++03 c++11; do for dbg in 0 1; do - for t in base_test space_optimized_test; do + for t in base_test space_optimized_test constant_erase_test; do g++ -std=$std -Wall -Wextra -DBOOST_CB_ENABLE_DEBUG=$dbg $INC test/$t.cpp -o /tmp/$t && /tmp/$t || echo "FAIL $t $std dbg=$dbg" done done done # debug-off only: -for t in soft_iterator_invalidation constant_erase_test; do - g++ -std=c++11 $INC test/$t.cpp -o /tmp/$t && /tmp/$t || echo "FAIL $t" -done +g++ -std=c++11 $INC test/soft_iterator_invalidation.cpp -o /tmp/sii && /tmp/sii || echo "FAIL soft_iterator_invalidation" ``` `bounded_buffer_comparison.cpp` and `circular_buffer_bound_example.cpp` need @@ -200,7 +208,7 @@ to `boost-root/doc/html/circular_buffer.html`. | Stage | Tool | Source | |---|---|---| | Narrative | Quickbook 1.6 | `doc/circular_buffer.qbk` | -| API reference | Doxygen, then BoostBook | `/*! */` comments in `circular_buffer.hpp`, `base.hpp`, `space_optimized.hpp` (emitted as `autodoc.xml`) | +| API reference | Doxygen, then BoostBook | `/*! */` comments in `circular_buffer.hpp`, `circular_buffer_fwd.hpp`, `base.hpp`, `space_optimized.hpp` (emitted as `autodoc.xml`) | | Index | AutoIndex | `doc/circular_buffer.idx` | | Rendering | BoostBook XSL (xsltproc; FOP for PDF) | the combined DocBook | diff --git a/doc/circular_buffer.qbk b/doc/circular_buffer.qbk index 65f0561a..fd4be695 100644 --- a/doc/circular_buffer.qbk +++ b/doc/circular_buffer.qbk @@ -473,6 +473,17 @@ Pull requests are welcome at [@https://github.com/boostorg/circular_buffer boost [section:release Release Notes] +[h4 Boost 1.94] + +* `erase_begin(size_type)`, `erase_end(size_type)`, `clear()` and the destructor now have constant complexity for + all trivially destructible types, not only scalars (`has_trivial_destructor` instead of `is_scalar`). +* The constant-time path skips the allocator's `destroy()` as well as the destructor, so it is now only used with + the default allocator. With any other allocator each element is destroyed through the allocator, so an allocator + with its own `destroy()` sees every call; for scalars this makes these operations linear where they were constant. + Specialize `boost::circular_buffer_trivial_destroy` to derive from `boost::true_type` to use the + constant-time path with an allocator that does not need `destroy()` calls for trivially destructible elements. +* Added `boost::circular_buffer_default_allocator::type`, the allocator used when none is specified. + [h4 Boost 1.91] * Avoided a GCC 4.x warning about calling `memset` with a constant size of zero. diff --git a/doc/jamfile.v2 b/doc/jamfile.v2 index b4dc2798..40c48f75 100644 --- a/doc/jamfile.v2 +++ b/doc/jamfile.v2 @@ -27,6 +27,7 @@ doxygen autodoc : # List all the files individually (RECURSIVE=NO ). [ glob ../include/boost/circular_buffer.hpp ] + [ glob ../include/boost/circular_buffer_fwd.hpp ] [ glob ../include/boost/circular_buffer/base.hpp ] [ glob ../include/boost/circular_buffer/space_optimized.hpp ] @@ -65,12 +66,15 @@ doxygen autodoc \"BOOST_NOEXCEPT_IF(T)=noexcept(T)\" \\ \"BOOST_ASSERT(expr)=\" \\ \"BOOST_RV_REF(T)=T &&\" \\ + \"BOOST_CB_DEFAULT_ALLOCATOR(T):=std::allocator\" \\ \"ASSERT(x)=assert(x)\" \\ \"__cplusplus \"" # BOOST_PREVENT_MACRO_SUBSTITUTION, will not be replaced by , # BOOST_STATIC_CONSTANT will be replaced by "static x const y", # BOOST_DEDUCED_TYPENAME will be replaced by "typename", # BOOST_CONSTEXPR will be replaced by "constexpr". + # BOOST_CB_DEFAULT_ALLOCATOR(T) will be replaced by "std::allocator" (":=" so the #define and #undef in + # circular_buffer_fwd.hpp do not override it). EXCLUDE_SYMBOLS=*_throws # IMAGE_PATH="../images" # for circular_buffer.svg # See autodoxywarnings.log to check this is correct. diff --git a/include/boost/circular_buffer/base.hpp b/include/boost/circular_buffer/base.hpp index 47441544..bd249593 100644 --- a/include/boost/circular_buffer/base.hpp +++ b/include/boost/circular_buffer/base.hpp @@ -19,16 +19,18 @@ #endif #include +#include #include #include #include #include #include #include -#include #include #include #include +#include +#include #include #include #include @@ -1189,7 +1191,8 @@ private empty_value Invalidates all iterators pointing to the circular_buffer (including iterators equal to end()). \par Complexity - Constant (in the size of the circular_buffer) for scalar types; linear for other types. + Constant (in the size of the circular_buffer) for trivially destructible types with the + default allocator (see circular_buffer_trivial_destroy); linear otherwise. \sa clear() */ ~circular_buffer() BOOST_NOEXCEPT { @@ -2251,7 +2254,7 @@ private empty_value return iterator(this, last.m_it); } - //! Remove first n elements (with constant complexity for scalar types). + //! Remove first n elements (with constant complexity for trivially destructible types). /*! \pre n \<= size() \post The n elements at the beginning of the circular_buffer will be removed. @@ -2259,16 +2262,19 @@ private empty_value \throws Exceptions of move_if_noexcept(T&). \par Exception Safety Basic; no-throw if the operation in the Throws section does not throw anything. (I.e. no throw in - case of scalars.) + case of trivially destructible types.) \par Iterator Invalidation Invalidates iterators pointing to the first n erased elements. \par Complexity - Constant (in n) for scalar types; linear for other types. - \note This method has been specially designed for types which do not require an explicit destructruction (e.g. - integer, float or a pointer). For these scalar types a call to a destructor is not required which makes - it possible to implement the "erase from beginning" operation with a constant complexity. For non-sacalar - types the complexity is linear (hence the explicit destruction is needed) and the implementation is - actually equivalent to + Constant (in n) for trivially destructible types with the default allocator (see + circular_buffer_trivial_destroy); linear otherwise. + \note This method has been specially designed for types which do not require an explicit destruction (e.g. + integer, float, a pointer or a class with a trivial destructor). For these trivially destructible types + a call to a destructor is not required which makes it possible to implement the "erase from beginning" + operation with a constant complexity, without calling destroy() on the allocator. This + applies to the default allocator, and to any other allocator for which + circular_buffer_trivial_destroy is specialized. In all other cases the complexity is linear + (hence the explicit destruction is needed) and the implementation is actually equivalent to \link circular_buffer::rerase(iterator, iterator) rerase(begin(), begin() + n)\endlink. \sa erase(iterator), erase(iterator, iterator), rerase(iterator), rerase(iterator, iterator), @@ -2279,11 +2285,11 @@ private empty_value #if BOOST_CB_ENABLE_DEBUG erase_begin(n, false_type()); #else - erase_begin(n, is_scalar()); + erase_begin(n, trivial_erase()); #endif } - //! Remove last n elements (with constant complexity for scalar types). + //! Remove last n elements (with constant complexity for trivially destructible types). /*! \pre n \<= size() \post The n elements at the end of the circular_buffer will be removed. @@ -2291,16 +2297,19 @@ private empty_value \throws Exceptions of move_if_noexcept(T&). \par Exception Safety Basic; no-throw if the operation in the Throws section does not throw anything. (I.e. no throw in - case of scalars.) + case of trivially destructible types.) \par Iterator Invalidation Invalidates iterators pointing to the last n erased elements. \par Complexity - Constant (in n) for scalar types; linear for other types. - \note This method has been specially designed for types which do not require an explicit destructruction (e.g. - integer, float or a pointer). For these scalar types a call to a destructor is not required which makes - it possible to implement the "erase from end" operation with a constant complexity. For non-sacalar - types the complexity is linear (hence the explicit destruction is needed) and the implementation is - actually equivalent to + Constant (in n) for trivially destructible types with the default allocator (see + circular_buffer_trivial_destroy); linear otherwise. + \note This method has been specially designed for types which do not require an explicit destruction (e.g. + integer, float, a pointer or a class with a trivial destructor). For these trivially destructible types + a call to a destructor is not required which makes it possible to implement the "erase from end" + operation with a constant complexity, without calling destroy() on the allocator. This + applies to the default allocator, and to any other allocator for which + circular_buffer_trivial_destroy is specialized. In all other cases the complexity is linear + (hence the explicit destruction is needed) and the implementation is actually equivalent to \link circular_buffer::erase(iterator, iterator) erase(end() - n, end())\endlink. \sa erase(iterator), erase(iterator, iterator), rerase(iterator), rerase(iterator, iterator), @@ -2311,7 +2320,7 @@ private empty_value #if BOOST_CB_ENABLE_DEBUG erase_end(n, false_type()); #else - erase_end(n, is_scalar()); + erase_end(n, trivial_erase()); #endif } @@ -2325,7 +2334,8 @@ private empty_value Invalidates all iterators pointing to the circular_buffer (except iterators equal to end()). \par Complexity - Constant (in the size of the circular_buffer) for scalar types; linear for other types. + Constant (in the size of the circular_buffer) for trivially destructible types with the + default allocator (see circular_buffer_trivial_destroy); linear otherwise. \sa ~circular_buffer(), erase(iterator), erase(iterator, iterator), rerase(iterator), rerase(iterator, iterator), erase_begin(size_type), erase_end(size_type) @@ -2338,6 +2348,12 @@ private empty_value private: // Helper methods + /*! INTERNAL ONLY */ + // Whether erasing can skip both the destructor and the allocator's destroy() (see + // circular_buffer_trivial_destroy). Selects the constant-time erase_begin(), erase_end() and destroy_content(). + typedef integral_constant::value && circular_buffer_trivial_destroy::value> trivial_erase; + /*! INTERNAL ONLY */ void check_position(size_type index) const { if (index >= size()) @@ -2462,7 +2478,7 @@ private empty_value #if BOOST_CB_ENABLE_DEBUG destroy_content(false_type()); #else - destroy_content(is_scalar()); + destroy_content(trivial_erase()); #endif } diff --git a/include/boost/circular_buffer_fwd.hpp b/include/boost/circular_buffer_fwd.hpp index 621fb953..a0724efa 100644 --- a/include/boost/circular_buffer_fwd.hpp +++ b/include/boost/circular_buffer_fwd.hpp @@ -16,6 +16,8 @@ #endif #include +#include +#include #if !defined(BOOST_NO_STD_ALLOCATOR) #include #else @@ -30,14 +32,43 @@ namespace boost { #define BOOST_CB_DEFAULT_ALLOCATOR(T) BOOST_DEDUCED_TYPENAME std::vector::allocator_type #endif -template +//! The default allocator of circular_buffer and circular_buffer_space_optimized. +template +struct circular_buffer_default_allocator { + //! The allocator used for elements of type T when none is specified. + typedef BOOST_CB_DEFAULT_ALLOCATOR(T) type; +}; + +#undef BOOST_CB_DEFAULT_ALLOCATOR + +//! Whether elements with a trivial destructor can be erased without calling the allocator's destroy(). +/*! + When this is true and the stored type has a trivial destructor, erase_begin(), + erase_end(), clear() and the destructor of circular_buffer run in constant + time and do not call destroy() on the allocator for the erased elements. Otherwise each element is + destroyed through the allocator and these operations are linear. + + It is true only for the default allocator, whose destroy() does nothing more than call + the destructor. Specialize it to derive from boost::true_type for an allocator that does not need + destroy() calls for elements with a trivial destructor: + \code + namespace boost { + template + struct circular_buffer_trivial_destroy > : true_type {}; + } + \endcode +*/ +template +struct circular_buffer_trivial_destroy + : is_same::type> {}; + +template ::type> class circular_buffer; -template +template ::type> class circular_buffer_space_optimized; -#undef BOOST_CB_DEFAULT_ALLOCATOR - } // namespace boost #endif // #if !defined(BOOST_CIRCULAR_BUFFER_FWD_HPP) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index a1742c52..930e5606 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,4 +27,5 @@ run base_test.cpp : : : "BOOST_CB_ENABLE_DEBUG=1" : base_test_dbg ; run space_optimized_test.cpp : : : "BOOST_CB_ENABLE_DEBUG=1" : space_optimized_test_dbg ; run soft_iterator_invalidation.cpp ; run constant_erase_test.cpp ; +run constant_erase_test.cpp : : : "BOOST_CB_ENABLE_DEBUG=1" : constant_erase_test_dbg ; compile bounded_buffer_comparison.cpp : multi /boost/thread//boost_thread /boost/timer//boost_timer ; diff --git a/test/constant_erase_test.cpp b/test/constant_erase_test.cpp index e2431be2..b1a78acf 100644 --- a/test/constant_erase_test.cpp +++ b/test/constant_erase_test.cpp @@ -7,10 +7,18 @@ // http://www.boost.org/LICENSE_1_0.txt) #include "test.hpp" +#include +#include +#include +#include int MyInteger::ms_exception_trigger = 0; int InstanceCounter::ms_count = 0; +// Erasing a trivially destructible element leaves its memory untouched, so the old value can still be read +// through a pointer. Debug mode (BOOST_CB_ENABLE_DEBUG) overwrites erased memory, so those checks use +// CB_TEST_DEBUG_EXCLUDE. + void erase_begin_test() { circular_buffer cb1(5); @@ -32,9 +40,9 @@ void erase_begin_test() { cb1.erase_begin(3); BOOST_TEST(cb1.empty()); - BOOST_TEST(*p == 2); - BOOST_TEST(*(p + 1) == 3); - BOOST_TEST(*(p + 2) == 4); + CB_TEST_DEBUG_EXCLUDE(*p == 2); + CB_TEST_DEBUG_EXCLUDE(*(p + 1) == 3); + CB_TEST_DEBUG_EXCLUDE(*(p + 2) == 4); cb1.push_back(10); cb1.push_back(11); @@ -68,6 +76,31 @@ void erase_begin_test() { BOOST_TEST(cb3[0] == 4); BOOST_TEST(cb3[1] == 5); BOOST_TEST(cb3[2] == 6); + + + circular_buffer cb4(5); + cb4.push_back(1); + cb4.push_back(2); + cb4.push_back(3); + cb4.push_back(4); + cb4.push_back(5); + cb4.push_back(6); + + + circular_buffer::pointer p4 = &cb4[0]; + cb4.erase_begin(5); + BOOST_TEST(cb4.empty()); + CB_TEST_DEBUG_EXCLUDE(p4->m_n == 2); + CB_TEST_DEBUG_EXCLUDE((p4 + 1)->m_n == 3); + CB_TEST_DEBUG_EXCLUDE((p4 + 2)->m_n == 4); + CB_TEST_DEBUG_EXCLUDE((p4 + 3)->m_n == 5); + CB_TEST_DEBUG_EXCLUDE((p4 - 1)->m_n == 6); + + cb4.push_back(10); + cb4.push_back(11); + BOOST_TEST(cb4.size() == 2); + BOOST_TEST(cb4[0].m_n == 10); + BOOST_TEST(cb4[1].m_n == 11); } void erase_end_test() { @@ -91,9 +124,9 @@ void erase_end_test() { cb1.erase_end(3); BOOST_TEST(cb1.empty()); - BOOST_TEST(*p == 5); - BOOST_TEST(*(p - 1) == 4); - BOOST_TEST(*(p - 2) == 3); + CB_TEST_DEBUG_EXCLUDE(*p == 5); + CB_TEST_DEBUG_EXCLUDE(*(p - 1) == 4); + CB_TEST_DEBUG_EXCLUDE(*(p - 2) == 3); cb1.push_back(10); cb1.push_back(11); @@ -127,6 +160,31 @@ void erase_end_test() { BOOST_TEST(cb3[0] == 2); BOOST_TEST(cb3[1] == 3); BOOST_TEST(cb3[2] == 4); + + + circular_buffer cb4(5); + cb4.push_back(1); + cb4.push_back(2); + cb4.push_back(3); + cb4.push_back(4); + cb4.push_back(5); + cb4.push_back(6); + + circular_buffer::pointer p4 = &cb4[0]; + + cb4.erase_end(5); + BOOST_TEST(cb4.empty()); + CB_TEST_DEBUG_EXCLUDE(p4->m_n == 2); + CB_TEST_DEBUG_EXCLUDE((p4 + 1)->m_n == 3); + CB_TEST_DEBUG_EXCLUDE((p4 + 2)->m_n == 4); + CB_TEST_DEBUG_EXCLUDE((p4 + 3)->m_n == 5); + CB_TEST_DEBUG_EXCLUDE((p4 - 1)->m_n == 6); + + cb4.push_back(10); + cb4.push_back(11); + BOOST_TEST(cb4.size() == 2); + BOOST_TEST(cb4[0].m_n == 10); + BOOST_TEST(cb4[1].m_n == 11); } void clear_test() { @@ -144,11 +202,11 @@ void clear_test() { cb1.clear(); BOOST_TEST(cb1.empty()); - BOOST_TEST(*p == 2); - BOOST_TEST(*(p + 1) == 3); - BOOST_TEST(*(p + 2) == 4); - BOOST_TEST(*(p + 3) == 5); - BOOST_TEST(*(p - 1) == 6); + CB_TEST_DEBUG_EXCLUDE(*p == 2); + CB_TEST_DEBUG_EXCLUDE(*(p + 1) == 3); + CB_TEST_DEBUG_EXCLUDE(*(p + 2) == 4); + CB_TEST_DEBUG_EXCLUDE(*(p + 3) == 5); + CB_TEST_DEBUG_EXCLUDE(*(p - 1) == 6); circular_buffer cb2(5, InstanceCounter()); @@ -170,6 +228,140 @@ void clear_test() { cb3.clear(); BOOST_TEST(cb3.empty()); + + circular_buffer cb4(5); + cb4.push_back(1); + cb4.push_back(2); + cb4.push_back(3); + cb4.push_back(4); + cb4.push_back(5); + cb4.push_back(6); + + circular_buffer::pointer p4 = &cb4[0]; + + cb4.clear(); + + BOOST_TEST(cb4.empty()); + CB_TEST_DEBUG_EXCLUDE(p4->m_n == 2); + CB_TEST_DEBUG_EXCLUDE((p4 + 1)->m_n == 3); + CB_TEST_DEBUG_EXCLUDE((p4 + 2)->m_n == 4); + CB_TEST_DEBUG_EXCLUDE((p4 + 3)->m_n == 5); + CB_TEST_DEBUG_EXCLUDE((p4 - 1)->m_n == 6); +} + +// An allocator with its own construct() and destroy(), which counts the calls. +int g_constructs = 0; +int g_destroys = 0; + +template +class counting_allocator : public std::allocator { +public: + typedef T value_type; + template struct rebind { typedef counting_allocator other; }; + counting_allocator() {} + template counting_allocator(const counting_allocator&) {} +#if !defined(BOOST_NO_CXX11_ALLOCATOR) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \ + !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + template + void construct(U* p, Args&&... args) { + ++g_constructs; + ::new (static_cast(p)) U(std::forward(args)...); + } + template + void destroy(U* p) { + ++g_destroys; + p->~U(); + } +#endif +}; + +// The same allocator, opted in to skipping destroy() for trivially destructible elements. +template +class opt_in_allocator : public counting_allocator { +public: + template struct rebind { typedef opt_in_allocator other; }; + opt_in_allocator() {} + template opt_in_allocator(const opt_in_allocator&) {} +}; + +namespace boost { +template +struct circular_buffer_trivial_destroy > : true_type {}; +} // namespace boost + +// Only the default allocator, or an allocator that opts in, may skip destroy(). +BOOST_STATIC_ASSERT(circular_buffer_trivial_destroy >::value); +BOOST_STATIC_ASSERT(circular_buffer_trivial_destroy::type>::value); +BOOST_STATIC_ASSERT(!circular_buffer_trivial_destroy >::value); +BOOST_STATIC_ASSERT(circular_buffer_trivial_destroy >::value); + +// Count the allocator's destroy() calls for erase_begin(), erase_end(), clear() and the destructor. When +// "skipped" is true they take the constant-time path, which does not call destroy(), except in debug mode, which +// always destroys each element. +template +void check_allocator_destroy(bool skipped) { + const bool fast = skipped && !BOOST_CB_ENABLE_DEBUG; + g_constructs = g_destroys = 0; + { + circular_buffer cb(8); + for (int i = 0; i < 8; ++i) + cb.push_back(T(i)); + BOOST_TEST_EQ(g_constructs, 8); + + int before = g_destroys; + cb.erase_begin(2); + BOOST_TEST_EQ(g_destroys - before, fast ? 0 : 2); + + before = g_destroys; + cb.erase_end(1); + BOOST_TEST_EQ(g_destroys - before, fast ? 0 : 1); + + before = g_destroys; + cb.pop_front(); // Always destroys the element. + BOOST_TEST_EQ(g_destroys - before, 1); + + before = g_destroys; + cb.clear(); + BOOST_TEST_EQ(g_destroys - before, fast ? 0 : 4); + + for (int i = 0; i < 3; ++i) + cb.push_back(T(i)); + } + BOOST_TEST_EQ(g_destroys, fast ? 1 : g_constructs); // Destructor: 3 more, unless skipped. +} + +// Every element constructed through the allocator is destroyed through it. +template +void check_space_optimized_allocator_destroy() { + g_constructs = g_destroys = 0; + { + circular_buffer_space_optimized cb(8); + for (int i = 0; i < 12; ++i) + cb.push_back(T(i)); + cb.erase(cb.begin(), cb.begin() + 2); + cb.erase(cb.end() - 1, cb.end()); + cb.clear(); + for (int i = 0; i < 3; ++i) + cb.push_back(T(i)); + } + BOOST_TEST(g_constructs > 0); + BOOST_TEST_EQ(g_constructs, g_destroys); +} + +void allocator_destroy_test() { +#if !defined(BOOST_NO_CXX11_ALLOCATOR) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \ + !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + // A custom allocator sees every destroy, even for trivially destructible types. + check_allocator_destroy >(false); + check_allocator_destroy >(false); + check_space_optimized_allocator_destroy >(); + check_space_optimized_allocator_destroy >(); + + // An opted-in allocator skips destroy() for trivially destructible types only. + check_allocator_destroy >(true); + check_allocator_destroy >(true); + check_allocator_destroy >(false); +#endif } // test main @@ -178,5 +370,6 @@ int main() erase_begin_test(); erase_end_test(); clear_test(); + allocator_destroy_test(); return boost::report_errors(); } diff --git a/test/test.hpp b/test/test.hpp index dfdd5de0..6b8a6460 100644 --- a/test/test.hpp +++ b/test/test.hpp @@ -24,6 +24,17 @@ #include #endif +// Checks that only hold with or without debug support (BOOST_CB_ENABLE_DEBUG). When a check is disabled, its +// expression is still named in an unevaluated sizeof, so variables used only by the check don't trigger +// unused-variable warnings. +#if BOOST_CB_ENABLE_DEBUG + #define CB_TEST_DEBUG_ONLY(expr) BOOST_TEST(expr) + #define CB_TEST_DEBUG_EXCLUDE(expr) ((void)sizeof(expr)) +#else + #define CB_TEST_DEBUG_ONLY(expr) ((void)sizeof(expr)) + #define CB_TEST_DEBUG_EXCLUDE(expr) BOOST_TEST(expr) +#endif + // Integer (substitute for int) - more appropriate for testing class MyInteger { private: