diff --git a/include/boost/container/stable_vector.hpp b/include/boost/container/stable_vector.hpp index 5348b93..d790c47 100644 --- a/include/boost/container/stable_vector.hpp +++ b/include/boost/container/stable_vector.hpp @@ -56,6 +56,10 @@ namespace boost { namespace container { +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED namespace stable_vector_detail{ @@ -628,6 +632,24 @@ class stable_vector cod.release(); } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs a stable_vector that will use a copy of allocator a + //! and inserts a copy of the range [il.begin(), il.last()) in the stable_vector + //! + //! Throws: If allocator_type's default constructor + //! throws or T's constructor taking a dereferenced initializer_list iterator throws. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + stable_vector(std::initializer_list il, const allocator_type& l = allocator_type()) + : internal_data(l), index(l) + { + stable_vector_detail::clear_on_destroy cod(*this); + insert(cend(), il.begin(), il.end()) + STABLE_VECTOR_CHECK_INVARIANT; + cod.release(); + } +#endif + //! Effects: Move constructor. Moves mx's resources to *this. //! //! Throws: If allocator_type's copy constructor throws. @@ -753,6 +775,20 @@ class stable_vector return *this; } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Make *this container contains elements from il. + //! + //! Complexity: Linear to the range [il.begin(), il.end()). + stable_vector& operator=(std::initializer_list il) + { + STABLE_VECTOR_CHECK_INVARIANT; + clear(); + shrink_to_fit(); + assign(il.begin(), il.end()); + return *this; + } +#endif + //! Effects: Assigns the n copies of val to *this. //! //! Throws: If memory allocation throws or T's copy constructor throws. @@ -792,6 +828,19 @@ class stable_vector } } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assigns the the range [il.begin(), il.end()) to *this. + //! + //! Throws: If memory allocation throws or + //! T's constructor from dereferencing initializer_list iterator throws. + //! + void assign(std::initializer_list il) + { + STABLE_VECTOR_CHECK_INVARIANT; + assign(il.begin(), il.end()); + } +#endif + //! Effects: Returns a copy of the internal allocator. //! //! Throws: If allocator's copy constructor throws. @@ -1345,6 +1394,21 @@ class stable_vector return this->insert(position, cvalue_iterator(t, n), cvalue_iterator()); } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Requires: position must be a valid iterator of *this. + //! + //! Effects: Insert a copy of the [il.begin(), il.end()) range before position. + //! + //! Returns: an iterator to the first inserted element or position if first == last. + //! + //! Complexity: Linear to std::distance [il.begin(), il.end()). + iterator insert(const_iterator position, std::initializer_list il) + { + STABLE_VECTOR_CHECK_INVARIANT; + return insert(position, il.begin(), il.end()); + } +#endif + //! Requires: pos must be a valid iterator of *this. //! //! Effects: Insert a copy of the [first, last) range before pos. diff --git a/test/stable_vector_test.cpp b/test/stable_vector_test.cpp index 9abda43..f8c9a7e 100644 --- a/test/stable_vector_test.cpp +++ b/test/stable_vector_test.cpp @@ -112,6 +112,44 @@ int test_cont_variants() return 0; } +bool test_methods_with_initializer_list_as_argument() +{ +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + { + const stable_vector testedVector = {1, 2, 3}; + const std::vector expectedVector = {1, 2, 3}; + if(!test::CheckEqualContainers(&testedVector, &expectedVector)) return false; + } + + { + stable_vector testedVector = {1, 2, 3}; + testedVector = {11, 12, 13}; + + const std::vector expectedVector = {11, 12, 13}; + if(!test::CheckEqualContainers(&testedVector, &expectedVector)) return false; + } + + { + stable_vector testedVector = {1, 2, 3}; + testedVector.assign({5, 6, 7}); + + const std::vector expectedVector = {5, 6, 7}; + if(!test::CheckEqualContainers(&testedVector, &expectedVector)) return false; + } + + { + stable_vector testedVector = {1, 2, 3}; + testedVector.insert(testedVector.cend(), {5, 6, 7}); + + const std::vector expectedVector = {1, 2, 3, 5, 6, 7}; + if(!test::CheckEqualContainers(&testedVector, &expectedVector)) return false; + } + return true; +#else + return true; +#endif +} + int main() { recursive_vector_test(); @@ -178,6 +216,12 @@ int main() if(!boost::container::test::test_propagate_allocator()) return 1; + if(!test_methods_with_initializer_list_as_argument()) + { + std::cerr << "test_methods_with_initializer_list_as_argument failed" << std::endl; + return 1; + } + return 0; }