xref: /freebsd/contrib/llvm-project/libcxx/include/__iterator/size.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 <__type_traits/common_type.h>
15 #include <__type_traits/make_signed.h>
16 #include <cstddef>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 #if _LIBCPP_STD_VER >= 17
25 
26 template <class _Cont>
27 _LIBCPP_HIDE_FROM_ABI constexpr auto size(const _Cont& __c) noexcept(noexcept(__c.size())) -> decltype(__c.size()) {
28   return __c.size();
29 }
30 
31 template <class _Tp, size_t _Sz>
size(const _Tp (&)[_Sz])32 _LIBCPP_HIDE_FROM_ABI constexpr size_t size(const _Tp (&)[_Sz]) noexcept {
33   return _Sz;
34 }
35 
36 #  if _LIBCPP_STD_VER >= 20
37 template <class _Cont>
38 _LIBCPP_HIDE_FROM_ABI constexpr auto
39 ssize(const _Cont& __c) noexcept(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(
40     __c.size()))) -> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>> {
41   return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size());
42 }
43 
44 // GCC complains about the implicit conversion from ptrdiff_t to size_t in
45 // the array bound.
46 _LIBCPP_DIAGNOSTIC_PUSH
47 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wsign-conversion")
48 template <class _Tp, ptrdiff_t _Sz>
ssize(const _Tp (&)[_Sz])49 _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept {
50   return _Sz;
51 }
52 _LIBCPP_DIAGNOSTIC_POP
53 #  endif
54 
55 #endif // _LIBCPP_STD_VER >= 17
56 
57 _LIBCPP_END_NAMESPACE_STD
58 
59 #endif // _LIBCPP___ITERATOR_SIZE_H
60