diff --git a/include/boost/container/vector.hpp b/include/boost/container/vector.hpp index 16f52d3..e0d9dd5 100644 --- a/include/boost/container/vector.hpp +++ b/include/boost/container/vector.hpp @@ -50,6 +50,10 @@ #include #include +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + namespace boost { namespace container { @@ -710,6 +714,22 @@ class vector , x.size(), container_detail::to_raw_pointer(this->m_holder.start())); } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs a vector that will use a copy of allocator a + //! and inserts a copy of the range [il.begin(), il.last()) in the 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()). + vector(std::initializer_list il, const allocator_type& a = allocator_type()) + : m_holder(a) + { + insert(cend(), il.begin(), il.end()); + } +#endif + + //! Effects: Move constructor. Moves x's resources to *this. //! //! Throws: Nothing @@ -805,6 +825,17 @@ class 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()). + vector& operator=(std::initializer_list il) + { + assign(il.begin(), il.end()); + return *this; + } +#endif + //! Effects: Move assignment. All x's values are transferred to *this. //! //! Postcondition: x.empty(). *this contains a the elements x had @@ -905,6 +936,18 @@ class 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 iniializer_list iterator throws. + //! + void assign(std::initializer_list il) + { + assign(il.begin(), il.end()); + } +#endif + //! Effects: Assigns the the range [first, last) to *this. //! //! Throws: If memory allocation throws or T's copy/move constructor/assignment or @@ -1532,6 +1575,20 @@ class vector } #endif +#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 the range [il.begin(), il.end()). + iterator insert(const_iterator position, std::initializer_list il) + { + return insert(position, il.begin(), il.end()); + } +#endif + //! Effects: Removes the last element from the vector. //! //! Throws: Nothing. diff --git a/test/stable_vector_test.cpp b/test/stable_vector_test.cpp index cbc3efb..4e96ea1 100644 --- a/test/stable_vector_test.cpp +++ b/test/stable_vector_test.cpp @@ -112,44 +112,6 @@ 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(); @@ -219,7 +181,9 @@ int main() //////////////////////////////////// // Initializer lists testing //////////////////////////////////// - if(!test_methods_with_initializer_list_as_argument()) + if(!boost::container::test::test_vector_methods_with_initializer_list_as_argument_for< + boost::container::stable_vector + >()) { std::cerr << "test_methods_with_initializer_list_as_argument failed" << std::endl; return 1; diff --git a/test/vector_test.cpp b/test/vector_test.cpp index ab7a4a4..614e17c 100644 --- a/test/vector_test.cpp +++ b/test/vector_test.cpp @@ -251,6 +251,14 @@ int main() return 1; } + //////////////////////////////////// + // Initializer lists testing + //////////////////////////////////// + if(!boost::container::test::test_vector_methods_with_initializer_list_as_argument_for< + boost::container::vector + >()) { + return 1; + } return 0; } diff --git a/test/vector_test.hpp b/test/vector_test.hpp index 8a5ccdd..a8c4bf9 100644 --- a/test/vector_test.hpp +++ b/test/vector_test.hpp @@ -328,6 +328,45 @@ int vector_test() return 0; } +template +bool test_vector_methods_with_initializer_list_as_argument_for() +{ +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + { + const VectorContainerType testedVector = {1, 2, 3}; + const std::vector expectedVector = {1, 2, 3}; + if(!test::CheckEqualContainers(&testedVector, &expectedVector)) return false; + } + + { + VectorContainerType testedVector = {1, 2, 3}; + testedVector = {11, 12, 13}; + + const std::vector expectedVector = {11, 12, 13}; + if(!test::CheckEqualContainers(&testedVector, &expectedVector)) return false; + } + + { + VectorContainerType testedVector = {1, 2, 3}; + testedVector.assign({5, 6, 7}); + + const std::vector expectedVector = {5, 6, 7}; + if(!test::CheckEqualContainers(&testedVector, &expectedVector)) return false; + } + + { + VectorContainerType 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 +} + } //namespace test{ } //namespace container { } //namespace boost{