size.pass.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. // <iterator>
  10. // template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
  11. // template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
  12. #include <iterator>
  13. #include <cassert>
  14. #include <vector>
  15. #include <array>
  16. #include <list>
  17. #include <initializer_list>
  18. #include "test_macros.h"
  19. #if TEST_STD_VER > 14
  20. #include <string_view>
  21. #endif
  22. template<typename C>
  23. void test_container( C& c)
  24. {
  25. // Can't say noexcept here because the container might not be
  26. assert ( std::size(c) == c.size());
  27. }
  28. template<typename C>
  29. void test_const_container( const C& c )
  30. {
  31. // Can't say noexcept here because the container might not be
  32. assert ( std::size(c) == c.size());
  33. }
  34. template<typename T>
  35. void test_const_container( const std::initializer_list<T>& c)
  36. {
  37. LIBCPP_ASSERT_NOEXCEPT(std::size(c)); // our std::size is conditionally noexcept
  38. assert ( std::size(c) == c.size());
  39. }
  40. template<typename T>
  41. void test_container( std::initializer_list<T>& c )
  42. {
  43. LIBCPP_ASSERT_NOEXCEPT(std::size(c)); // our std::size is conditionally noexcept
  44. assert ( std::size(c) == c.size());
  45. }
  46. template<typename T, size_t Sz>
  47. void test_const_array( const T (&array)[Sz] )
  48. {
  49. ASSERT_NOEXCEPT(std::size(array));
  50. assert ( std::size(array) == Sz );
  51. }
  52. int main(int, char**)
  53. {
  54. std::vector<int> v; v.push_back(1);
  55. std::list<int> l; l.push_back(2);
  56. std::array<int, 1> a; a[0] = 3;
  57. std::initializer_list<int> il = { 4 };
  58. test_container ( v );
  59. test_container ( l );
  60. test_container ( a );
  61. test_container ( il );
  62. test_const_container ( v );
  63. test_const_container ( l );
  64. test_const_container ( a );
  65. test_const_container ( il );
  66. #if TEST_STD_VER > 14
  67. std::string_view sv{"ABC"};
  68. test_container ( sv );
  69. test_const_container ( sv );
  70. #endif
  71. static constexpr int arrA [] { 1, 2, 3 };
  72. test_const_array ( arrA );
  73. return 0;
  74. }