/* Copyright 2019 Glen Joseph Fernandes (glenjofe@gmail.com) Distributed under the Boost Software License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) */ #include #include #include #include template class creator : public boost::default_allocator { public: template struct rebind { typedef creator other; }; creator(int* count) : count_(count) { } template creator(const creator& other) : count_(other.count()) { } template void construct(U*, const V&) { ++(*count_); } template void destroy(U*) { --(*count_); } int* count() const { return count_; } private: int* count_; }; template inline bool operator==(const creator& lhs, const creator& rhs) { return lhs.count() == rhs.count(); } template inline bool operator!=(const creator& lhs, const creator& rhs) { return !(lhs == rhs); } class type { public: type() { } type(int value) : value_(value) { } int value() const { return value_; } private: int value_; }; int main() { { int c = 0; std::vector > > v(&c); BOOST_TEST(c == 0); v.push_back(1); BOOST_TEST(v.front() == 1); v.push_back(2); BOOST_TEST(c == 0); v.clear(); BOOST_TEST(c == 0); v.resize(5); BOOST_TEST(c == 0); v.front() = 1; } { int c = 0; std::vector > > v(&c); BOOST_TEST(c == 0); v.push_back(1); BOOST_TEST(v.front().value() == 1); v.push_back(2); BOOST_TEST(c == 0); v.clear(); BOOST_TEST(c == 0); v.resize(5); BOOST_TEST(c == 0); v.front() = 1; } return boost::report_errors(); }