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 template<typename C> 26 void test_const_container( const C& c ) 27 { 28 // Can't say noexcept here because the container might not be 29 assert ( std::size(c) == c.size()); 30 } 31 32 template<typename T> 33 void test_const_container( const std::initializer_list<T>& c) 34 { 35 // ASSERT_NOEXCEPT(std::size(c)); 36 // For some reason, there isn't a std::size() for initializer lists 37 assert ( std::size(c) == c.size()); 38 } 39 40 template<typename C> 41 void test_container( C& c) 42 { 43 // Can't say noexcept here because the container might not be 44 assert ( std::size(c) == c.size()); 45 } 46 47 template<typename T> 48 void test_container( std::initializer_list<T>& c ) 49 { 50 // ASSERT_NOEXCEPT(std::size(c)); 51 // For some reason, there isn't a std::size() for initializer lists 52 assert ( std::size(c) == c.size()); 53 } 54 55 template<typename T, size_t Sz> 56 void test_const_array( const T (&array)[Sz] ) 57 { 58 ASSERT_NOEXCEPT(std::size(array)); 59 assert ( std::size(array) == Sz ); 60 } 61 62 int main() 63 { 64 std::vector<int> v; v.push_back(1); 65 std::list<int> l; l.push_back(2); 66 std::array<int, 1> a; a[0] = 3; 67 std::initializer_list<int> il = { 4 }; 68 69 test_container ( v ); 70 test_container ( l ); 71 test_container ( a ); 72 test_container ( il ); 73 74 test_const_container ( v ); 75 test_const_container ( l ); 76 test_const_container ( a ); 77 test_const_container ( il ); 78 79 static constexpr int arrA [] { 1, 2, 3 }; 80 test_const_array ( arrA ); 81 } 82