diff --git a/testing/vector.cu b/testing/vector.cu index de211af93..b09a4b55c 100644 --- a/testing/vector.cu +++ b/testing/vector.cu @@ -4,6 +4,9 @@ #include #include +#if THRUST_CPP_DIALECT >= 2011 +#include +#endif #include #include #include @@ -37,6 +40,30 @@ void TestVectorBool(void) } DECLARE_UNITTEST(TestVectorBool); +template +void TestVectorInitializerList(void) +{ + Vector v{1, 2, 3}; + ASSERT_EQUAL(v.size(), 3lu); + ASSERT_EQUAL(v[0], 1); + ASSERT_EQUAL(v[1], 2); + ASSERT_EQUAL(v[2], 3); + + v = {1, 2, 3, 4}; + ASSERT_EQUAL(v.size(), 4lu); + ASSERT_EQUAL(v[0], 1); + ASSERT_EQUAL(v[1], 2); + ASSERT_EQUAL(v[2], 3); + ASSERT_EQUAL(v[3], 4); + + const auto alloc = v.get_allocator(); + Vector v2{{1, 2, 3}, alloc}; + ASSERT_EQUAL(v2.size(), 3lu); + ASSERT_EQUAL(v2[0], 1); + ASSERT_EQUAL(v2[1], 2); + ASSERT_EQUAL(v2[2], 3); +} +DECLARE_VECTOR_UNITTEST(TestVectorInitializerList); template void TestVectorFrontBack(void) diff --git a/thrust/detail/vector_base.h b/thrust/detail/vector_base.h index b05f35194..df2edad18 100644 --- a/thrust/detail/vector_base.h +++ b/thrust/detail/vector_base.h @@ -28,6 +28,8 @@ #include #include #include + +#include #include THRUST_NAMESPACE_BEGIN @@ -123,12 +125,26 @@ template */ vector_base &operator=(const vector_base &v); - #if THRUST_CPP_DIALECT >= 2011 /*! Move assign operator moves from another vector_base. * \param v The vector_base to move. */ vector_base &operator=(vector_base &&v); - #endif + + /*! This constructor builds a \p vector_base from an intializer_list. + * \param il The intializer_list. + */ + vector_base(std::initializer_list il); + + /*! This constructor builds a \p vector_base from an intializer_list. + * \param il The intializer_list. + * \param alloc The allocator to use by this device_vector. + */ + vector_base(std::initializer_list il, const Alloc &alloc); + + /*! Assign operator copies from an initializer_list + * \param il The initializer_list. + */ + vector_base &operator=(std::initializer_list il); /*! Copy constructor copies from an exemplar vector_base with different * type. diff --git a/thrust/detail/vector_base.inl b/thrust/detail/vector_base.inl index ab94429a8..d9c29b0d2 100644 --- a/thrust/detail/vector_base.inl +++ b/thrust/detail/vector_base.inl @@ -195,6 +195,34 @@ template return *this; } // end vector_base::operator=() + template + vector_base + ::vector_base(std::initializer_list il) + :m_storage(), + m_size(0) + { + range_init(il.begin(), il.end()); + } // end vector_base::vector_base() + + template + vector_base + ::vector_base(std::initializer_list il, const Alloc &alloc) + :m_storage(alloc), + m_size(0) + { + range_init(il.begin(), il.end()); + } // end vector_base::vector_base() + + template + vector_base & + vector_base + ::operator=(std::initializer_list il) + { + assign(il.begin(), il.end()); + + return *this; + } // end vector_base::operator=() + template template void vector_base diff --git a/thrust/device_vector.h b/thrust/device_vector.h index b00251a0d..9b97e8d70 100644 --- a/thrust/device_vector.h +++ b/thrust/device_vector.h @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -197,6 +198,25 @@ template > device_vector &operator=(const detail::vector_base &v) { Parent::operator=(v); return *this; } + /*! This constructor builds a \p device_vector from an intializer_list. + * \param il The intializer_list. + */ + device_vector(std::initializer_list il) + :Parent(il) {} + + /*! This constructor builds a \p device_vector from an intializer_list. + * \param il The intializer_list. + * \param alloc The allocator to use by this device_vector. + */ + device_vector(std::initializer_list il, const Alloc &alloc) + :Parent(il, alloc) {} + + /*! Assign an \p intializer_list with a matching element type + * \param il The intializer_list. + */ + device_vector &operator=(std::initializer_list il) + { Parent::operator=(il); return *this; } + /*! This constructor builds a \p device_vector from a range. * \param first The beginning of the range. * \param last The end of the range. diff --git a/thrust/host_vector.h b/thrust/host_vector.h index 01bbceb3b..bb925ea9c 100644 --- a/thrust/host_vector.h +++ b/thrust/host_vector.h @@ -25,6 +25,8 @@ #include #include #include + +#include #include #include @@ -216,6 +218,25 @@ template > __host__ host_vector &operator=(const detail::vector_base &v) { Parent::operator=(v); return *this; } + + /*! This constructor builds a \p host_vector from an intializer_list. + * \param il The intializer_list. + */ + host_vector(std::initializer_list il) + :Parent(il) {} + + /*! This constructor builds a \p host_vector from an intializer_list. + * \param il The intializer_list. + * \param alloc The allocator to use by this host_vector. + */ + host_vector(std::initializer_list il, const Alloc &alloc) + :Parent(il, alloc) {} + + /*! Assign an \p intializer_list with a matching element type + * \param il The intializer_list. + */ + host_vector &operator=(std::initializer_list il) + { Parent::operator=(il); return *this; } /*! This constructor builds a \p host_vector from a range. * \param first The beginning of the range. diff --git a/thrust/system/cpp/detail/vector.inl b/thrust/system/cpp/detail/vector.inl index d27cdad64..02980c62a 100644 --- a/thrust/system/cpp/detail/vector.inl +++ b/thrust/system/cpp/detail/vector.inl @@ -98,6 +98,27 @@ template return *this; } #endif + + template + vector + ::vector(std::initializer_list il) + : super_t(il) + {} + + template + vector + ::vector(std::initializer_list il, const Allocator& alloc) + : super_t(il, alloc) + {} + + template + vector & + vector + ::operator=(std::initializer_list il) + { + super_t::operator=(il); + return *this; + } template template