1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++98, c++03, c++11, c++14 10 11 // <iterator> 12 // template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17 13 // template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17 14 15 #include <iterator> 16 #include <cassert> 17 #include <vector> 18 #include <array> 19 #include <list> 20 #include <initializer_list> 21 22 #include "test_macros.h" 23 24 #if TEST_STD_VER > 14 25 #include <string_view> 26 #endif 27 28 29 template<typename C> 30 void test_const_container( const C& c ) 31 { 32 // Can't say noexcept here because the container might not be 33 assert ( std::size(c) == c.size()); 34 } 35 36 template<typename T> 37 void test_const_container( const std::initializer_list<T>& c) 38 { 39 // ASSERT_NOEXCEPT(std::size(c)); 40 // For some reason, there isn't a std::size() for initializer lists 41 assert ( std::size(c) == c.size()); 42 } 43 44 template<typename C> 45 void test_container( C& c) 46 { 47 // Can't say noexcept here because the container might not be 48 assert ( std::size(c) == c.size()); 49 } 50 51 template<typename T> 52 void test_container( std::initializer_list<T>& c ) 53 { 54 // ASSERT_NOEXCEPT(std::size(c)); 55 // For some reason, there isn't a std::size() for initializer lists 56 assert ( std::size(c) == c.size()); 57 } 58 59 template<typename T, size_t Sz> 60 void test_const_array( const T (&array)[Sz] ) 61 { 62 ASSERT_NOEXCEPT(std::size(array)); 63 assert ( std::size(array) == Sz ); 64 } 65 66 int main(int, char**) 67 { 68 std::vector<int> v; v.push_back(1); 69 std::list<int> l; l.push_back(2); 70 std::array<int, 1> a; a[0] = 3; 71 std::initializer_list<int> il = { 4 }; 72 test_container ( v ); 73 test_container ( l ); 74 test_container ( a ); 75 test_container ( il ); 76 77 test_const_container ( v ); 78 test_const_container ( l ); 79 test_const_container ( a ); 80 test_const_container ( il ); 81 82 #if TEST_STD_VER > 14 83 std::string_view sv{"ABC"}; 84 test_container ( sv ); 85 test_const_container ( sv ); 86 #endif 87 88 static constexpr int arrA [] { 1, 2, 3 }; 89 test_const_array ( arrA ); 90 91 return 0; 92 } 93