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++03, c++11, c++14, c++17
10 
11 // <iterator>
12 // template <class C> constexpr auto ssize(const C& c)
13 //     -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>;                    // C++20
14 // template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
15 
16 #include <iterator>
17 #include <cassert>
18 #include <vector>
19 #include <array>
20 #include <list>
21 #include <initializer_list>
22 #include <string_view>
23 
24 #include "test_macros.h"
25 
26 // Ignore warning about std::numeric_limits comparisons being tautological.
27 #ifdef __GNUC__
28 #pragma GCC diagnostic ignored "-Wtype-limits"
29 #endif
30 
31 struct short_container {
sizeshort_container32     uint16_t size() const { return 60000; } // not noexcept
33 };
34 
35 template<typename C>
test_container(C & c)36 void test_container(C& c)
37 {
38 //  Can't say noexcept here because the container might not be
39     static_assert( std::is_signed_v<decltype(std::ssize(c))>, "");
40     assert ( std::ssize(c)   == static_cast<decltype(std::ssize(c))>(c.size()));
41 }
42 
43 template<typename C>
test_const_container(const C & c)44 void test_const_container(const C& c)
45 {
46 //  Can't say noexcept here because the container might not be
47     static_assert( std::is_signed_v<decltype(std::ssize(c))>, "");
48     assert ( std::ssize(c)   == static_cast<decltype(std::ssize(c))>(c.size()));
49 }
50 
51 template<typename T>
test_const_container(const std::initializer_list<T> & c)52 void test_const_container(const std::initializer_list<T>& c)
53 {
54     LIBCPP_ASSERT_NOEXCEPT(std::ssize(c)); // our std::ssize is conditionally noexcept
55     static_assert( std::is_signed_v<decltype(std::ssize(c))>, "");
56     assert ( std::ssize(c)   == static_cast<decltype(std::ssize(c))>(c.size()));
57 }
58 
59 template<typename T>
test_container(std::initializer_list<T> & c)60 void test_container(std::initializer_list<T>& c)
61 {
62     LIBCPP_ASSERT_NOEXCEPT(std::ssize(c)); // our std::ssize is conditionally noexcept
63     static_assert( std::is_signed_v<decltype(std::ssize(c))>, "");
64     assert ( std::ssize(c)   == static_cast<decltype(std::ssize(c))>(c.size()));
65 }
66 
67 template<typename T, size_t Sz>
test_const_array(const T (& array)[Sz])68 void test_const_array(const T (&array)[Sz])
69 {
70     ASSERT_NOEXCEPT(std::ssize(array));
71     static_assert( std::is_signed_v<decltype(std::ssize(array))>, "");
72     assert ( std::ssize(array) == Sz );
73 }
74 
main(int,char **)75 int main(int, char**)
76 {
77     std::vector<int> v; v.push_back(1);
78     std::list<int>   l; l.push_back(2);
79     std::array<int, 1> a; a[0] = 3;
80     std::initializer_list<int> il = { 4 };
81     test_container ( v );
82     ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(v)));
83     test_container ( l );
84     ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(l)));
85     test_container ( a );
86     ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(a)));
87     test_container ( il );
88     ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(il)));
89 
90     test_const_container ( v );
91     test_const_container ( l );
92     test_const_container ( a );
93     test_const_container ( il );
94 
95     std::string_view sv{"ABC"};
96     test_container ( sv );
97     ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(sv)));
98     test_const_container ( sv );
99 
100     static constexpr int arrA [] { 1, 2, 3 };
101     ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(arrA)));
102     static_assert( std::is_signed_v<decltype(std::ssize(arrA))>, "");
103     test_const_array ( arrA );
104 
105 //  From P1227R2:
106 //     Note that the code does not just return the std::make_signed variant of
107 //     the container's size() method, because it's conceivable that a container
108 //     might choose to represent its size as a uint16_t, supporting up to
109 //     65,535 elements, and it would be a disaster for std::ssize() to turn a
110 //     size of 60,000 into a size of -5,536.
111 
112     short_container sc;
113 //  is the return type signed? Is it big enough to hold 60K?
114 //  is the "signed version" of sc.size() too small?
115     static_assert( std::is_signed_v<                      decltype(std::ssize(sc))>, "");
116     static_assert( std::numeric_limits<                   decltype(std::ssize(sc))>::max()  > 60000, "");
117     static_assert( std::numeric_limits<std::make_signed_t<decltype(std:: size(sc))>>::max() < 60000, "");
118     assert (std::ssize(sc) == 60000);
119     LIBCPP_ASSERT_NOT_NOEXCEPT(std::ssize(sc));
120 
121   return 0;
122 }
123