xref: /llvm-project/libcxx/include/__iterator/size.h (revision e99c4906e44ae3f921fa05356909d006cda8d954)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___ITERATOR_SIZE_H
11 #define _LIBCPP___ITERATOR_SIZE_H
12 
13 #include <__config>
14 #include <__cstddef/ptrdiff_t.h>
15 #include <__cstddef/size_t.h>
16 #include <__type_traits/common_type.h>
17 #include <__type_traits/make_signed.h>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 #if _LIBCPP_STD_VER >= 17
26 
27 template <class _Cont>
28 _LIBCPP_HIDE_FROM_ABI constexpr auto size(const _Cont& __c) noexcept(noexcept(__c.size())) -> decltype(__c.size()) {
29   return __c.size();
30 }
31 
32 template <class _Tp, size_t _Sz>
33 _LIBCPP_HIDE_FROM_ABI constexpr size_t size(const _Tp (&)[_Sz]) noexcept {
34   return _Sz;
35 }
36 
37 #  if _LIBCPP_STD_VER >= 20
38 template <class _Cont>
39 _LIBCPP_HIDE_FROM_ABI constexpr auto
40 ssize(const _Cont& __c) noexcept(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(
41     __c.size()))) -> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>> {
42   return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size());
43 }
44 
45 // GCC complains about the implicit conversion from ptrdiff_t to size_t in
46 // the array bound.
47 _LIBCPP_DIAGNOSTIC_PUSH
48 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wsign-conversion")
49 template <class _Tp, ptrdiff_t _Sz>
50 _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept {
51   return _Sz;
52 }
53 _LIBCPP_DIAGNOSTIC_POP
54 #  endif
55 
56 #endif // _LIBCPP_STD_VER >= 17
57 
58 _LIBCPP_END_NAMESPACE_STD
59 
60 #endif // _LIBCPP___ITERATOR_SIZE_H
61