From ad75510453c7e7dbfb85b4b13520e33e8865b9c0 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 19 Nov 2014 19:43:23 +0000 Subject: [PATCH] Implement N4280 - 'Non-member size() and more' llvm-svn: 222378 --- libcxx/include/iterator | 41 +++++++++++ .../iterators/iterator.container/data.pass.cpp | 77 +++++++++++++++++++++ .../iterators/iterator.container/empty.pass.cpp | 80 ++++++++++++++++++++++ .../iterators/iterator.container/size.pass.cpp | 79 +++++++++++++++++++++ libcxx/www/cxx1z_status.html | 12 ++-- 5 files changed, 283 insertions(+), 6 deletions(-) create mode 100644 libcxx/test/iterators/iterator.container/data.pass.cpp create mode 100644 libcxx/test/iterators/iterator.container/empty.pass.cpp create mode 100644 libcxx/test/iterators/iterator.container/size.pass.cpp diff --git a/libcxx/include/iterator b/libcxx/include/iterator index 88e615a..bcf142a 100644 --- a/libcxx/include/iterator +++ b/libcxx/include/iterator @@ -324,6 +324,17 @@ template reverse_iterator rend(T (&array)[N]); // template auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14 template auto crend(const C& c) -> decltype(std::rend(c)); // C++14 +// 24.8, container access: +template constexpr auto size(const C& c) -> decltype(c.size()); // C++17 +template constexpr size_t size(const T (&array)[N]) noexcept; // C++17 +template constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17 +template constexpr bool empty(const T (&array)[N]) noexcept; // C++17 +template constexpr bool empty(initializer_list il) noexcept; // C++17 +template constexpr auto data(C& c) -> decltype(c.data()); // C++17 +template constexpr auto data(const C& c) -> decltype(c.data()); // C++17 +template constexpr T* data(T (&array)[N]) noexcept; // C++17 +template constexpr const E* data(initializer_list il) noexcept; // C++17 + } // std */ @@ -1568,6 +1579,36 @@ end(const _Cp& __c) #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN) +#if _LIBCPP_STD_VER > 14 +template +constexpr auto size(const _C& __c) -> decltype(__c.size()) { return __c.size(); } + +template +constexpr size_t size(const _Tp (&__array)[_N]) noexcept { return _N; } + +template +constexpr auto empty(const _C& __c) -> decltype(__c.empty()) { return __c.empty(); } + +template +constexpr bool empty(const _Tp (&__array)[_N]) noexcept { return false; } + +template +constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; } + +template constexpr +auto data(_C& __c) -> decltype(__c.data()) { return __c.data(); } + +template constexpr +auto data(const _C& __c) -> decltype(__c.data()) { return __c.data(); } + +template +constexpr _Tp* data(_Tp (&__array)[_N]) noexcept { return __array; } + +template +constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); } +#endif + + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_ITERATOR diff --git a/libcxx/test/iterators/iterator.container/data.pass.cpp b/libcxx/test/iterators/iterator.container/data.pass.cpp new file mode 100644 index 0000000..75ba950 --- /dev/null +++ b/libcxx/test/iterators/iterator.container/data.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// +// template constexpr auto data(C& c) -> decltype(c.data()); // C++17 +// template constexpr auto data(const C& c) -> decltype(c.data()); // C++17 +// template constexpr T* data(T (&array)[N]) noexcept; // C++17 +// template constexpr const E* data(initializer_list il) noexcept; // C++17 + +#if __cplusplus <= 201402L +int main () {} +#else + + +#include <__config> + +#include +#include +#include +#include +#include + +template +void test_const_container( const C& c ) +{ + assert ( std::data(c) == c.data()); +} + +template +void test_const_container( const std::initializer_list& c ) +{ + assert ( std::data(c) == c.begin()); +} + +template +void test_container( C& c ) +{ + assert ( std::data(c) == c.data()); +} + +template +void test_container( std::initializer_list& c) +{ + assert ( std::data(c) == c.begin()); +} + +template +void test_const_array( const T (&array)[Sz] ) +{ + assert ( std::data(array) == &array[0]); +} + +int main() +{ + std::vector v; v.push_back(1); + std::array a; a[0] = 3; + std::initializer_list il = { 4 }; + + test_container ( v ); + test_container ( a ); + test_container ( il ); + + test_const_container ( v ); + test_const_container ( a ); + test_const_container ( il ); + + static constexpr int arrA [] { 1, 2, 3 }; + test_const_array ( arrA ); +} + +#endif diff --git a/libcxx/test/iterators/iterator.container/empty.pass.cpp b/libcxx/test/iterators/iterator.container/empty.pass.cpp new file mode 100644 index 0000000..196a0e3 --- /dev/null +++ b/libcxx/test/iterators/iterator.container/empty.pass.cpp @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// +// template constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17 +// template constexpr bool empty(const T (&array)[N]) noexcept; // C++17 +// template constexpr bool empty(initializer_list il) noexcept; // C++17 + +#if __cplusplus <= 201402L +int main () {} +#else + + +#include <__config> + +#include +#include +#include +#include +#include +#include + +template +void test_const_container( const C& c ) +{ + assert ( std::empty(c) == c.empty()); +} + +template +void test_const_container( const std::initializer_list& c ) +{ + assert ( std::empty(c) == (c.size() == 0)); +} + +template +void test_container( C& c ) +{ + assert ( std::empty(c) == c.empty()); +} + +template +void test_container( std::initializer_list& c ) +{ + assert ( std::empty(c) == (c.size() == 0)); +} + +template +void test_const_array( const T (&array)[Sz] ) +{ + assert (!std::empty(array)); +} + +int main() +{ + std::vector v; v.push_back(1); + std::list l; l.push_back(2); + std::array a; a[0] = 3; + std::initializer_list il = { 4 }; + + test_container ( v ); + test_container ( l ); + test_container ( a ); + test_container ( il ); + + test_const_container ( v ); + test_const_container ( l ); + test_const_container ( a ); + test_const_container ( il ); + + static constexpr int arrA [] { 1, 2, 3 }; + test_const_array ( arrA ); +} + +#endif diff --git a/libcxx/test/iterators/iterator.container/size.pass.cpp b/libcxx/test/iterators/iterator.container/size.pass.cpp new file mode 100644 index 0000000..f8da9bf --- /dev/null +++ b/libcxx/test/iterators/iterator.container/size.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// +// template constexpr auto size(const C& c) -> decltype(c.size()); // C++17 +// template constexpr size_t size(const T (&array)[N]) noexcept; // C++17 + +#if __cplusplus <= 201402L +int main () {} +#else + + +#include <__config> + +#include +#include +#include +#include +#include +#include + +template +void test_const_container( const C& c ) +{ + assert ( std::size(c) == c.size()); +} + +template +void test_const_container( const std::initializer_list& c) +{ + assert ( std::size(c) == c.size()); +} + +template +void test_container( C& c) +{ + assert ( std::size(c) == c.size()); +} + +template +void test_container( std::initializer_list& c ) +{ + assert ( std::size(c) == c.size()); +} + +template +void test_const_array( const T (&array)[Sz] ) +{ + assert ( std::size(array) == Sz ); +} + +int main() +{ + std::vector v; v.push_back(1); + std::list l; l.push_back(2); + std::array a; a[0] = 3; + std::initializer_list il = { 4 }; + + test_container ( v ); + test_container ( l ); + test_container ( a ); + test_container ( il ); + + test_const_container ( v ); + test_const_container ( l ); + test_const_container ( a ); + test_const_container ( il ); + + static constexpr int arrA [] { 1, 2, 3 }; + test_const_array ( arrA ); +} + +#endif diff --git a/libcxx/www/cxx1z_status.html b/libcxx/www/cxx1z_status.html index 78bde38..caa750b 100644 --- a/libcxx/www/cxx1z_status.html +++ b/libcxx/www/cxx1z_status.html @@ -52,15 +52,15 @@ - N4190LWGRemoving auto_ptr, random_shuffle(), And Old Stuff.Urbana - N4284LWGContiguous Iterators.Urbana + N3911LWGTransformationTrait Alias void_t.UrbanaComplete3.6 N4089LWGSafe conversions in unique_ptr<T[]>.Urbana - N4277LWGTriviallyCopyable reference_wrapper.UrbanaComplete3.2 + N4169LWGA proposal to add invoke function templateUrbana + N4190LWGRemoving auto_ptr, random_shuffle(), And Old Stuff.Urbana N4258LWGCleaning-up noexcept in the Library.Urbana + N4277LWGTriviallyCopyable reference_wrapper.UrbanaComplete3.2 N4279LWGImproved insertion interface for unique-key maps.Urbana - N3911LWGTransformationTrait Alias void_t.UrbanaComplete3.6 - N4169LWGA proposal to add invoke function templateUrbana - N4280LWGNon-member size() and moreUrbana + N4280LWGNon-member size() and moreUrbanaComplete3.6 + N4284LWGContiguous Iterators.Urbana -- 2.7.4