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