Skip to content

Commit 7d3986e

Browse files
committed
Implement the second part of P1227R2 - Signed ssize() functions. Reviewed as https://reviews.llvm.org/D58642
llvm-svn: 354950
1 parent cba6eda commit 7d3986e

File tree

3 files changed

+146
-10
lines changed

3 files changed

+146
-10
lines changed

libcxx/include/iterator

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c));
399399
// 24.8, container access:
400400
template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
401401
template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
402+
403+
template <class C> constexpr auto ssize(const C& c)
404+
-> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
405+
template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
406+
402407
template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
403408
template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
404409
template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
@@ -1858,6 +1863,19 @@ template <class _Tp, size_t _Sz>
18581863
inline _LIBCPP_INLINE_VISIBILITY
18591864
constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
18601865

1866+
#if _LIBCPP_STD_VER > 17
1867+
template <class _Cont>
1868+
inline _LIBCPP_INLINE_VISIBILITY
1869+
constexpr auto ssize(const _Cont& __c)
1870+
_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
1871+
-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
1872+
{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
1873+
1874+
template <class _Tp, ptrdiff_t _Sz>
1875+
inline _LIBCPP_INLINE_VISIBILITY
1876+
constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1877+
#endif
1878+
18611879
template <class _Cont>
18621880
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
18631881
constexpr auto empty(const _Cont& __c)

libcxx/test/std/iterators/iterator.container/size.pass.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,30 @@
2727

2828

2929
template<typename C>
30-
void test_const_container( const C& c )
30+
void test_container( C& c)
3131
{
3232
// Can't say noexcept here because the container might not be
3333
assert ( std::size(c) == c.size());
3434
}
3535

36-
template<typename T>
37-
void test_const_container( const std::initializer_list<T>& c)
36+
template<typename C>
37+
void test_const_container( const C& c )
3838
{
39-
// ASSERT_NOEXCEPT(std::size(c));
40-
// For some reason, there isn't a std::size() for initializer lists
39+
// Can't say noexcept here because the container might not be
4140
assert ( std::size(c) == c.size());
4241
}
4342

44-
template<typename C>
45-
void test_container( C& c)
43+
template<typename T>
44+
void test_const_container( const std::initializer_list<T>& c)
4645
{
47-
// Can't say noexcept here because the container might not be
46+
LIBCPP_ASSERT_NOEXCEPT(std::size(c)); // our std::size is conditionally noexcept
4847
assert ( std::size(c) == c.size());
4948
}
5049

5150
template<typename T>
5251
void test_container( std::initializer_list<T>& c )
5352
{
54-
// ASSERT_NOEXCEPT(std::size(c));
55-
// For some reason, there isn't a std::size() for initializer lists
53+
LIBCPP_ASSERT_NOEXCEPT(std::size(c)); // our std::size is conditionally noexcept
5654
assert ( std::size(c) == c.size());
5755
}
5856

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

0 commit comments

Comments
 (0)