xref: /freebsd/contrib/llvm-project/libcxx/include/__tuple/find_index.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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 #ifndef _LIBCPP___TUPLE_FIND_INDEX_H
10 #define _LIBCPP___TUPLE_FIND_INDEX_H
11 
12 #include <__config>
13 #include <__type_traits/is_same.h>
14 #include <cstddef>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #  pragma GCC system_header
18 #endif
19 
20 #if _LIBCPP_STD_VER >= 14
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 namespace __find_detail {
25 
26 static constexpr size_t __not_found = static_cast<size_t>(-1);
27 static constexpr size_t __ambiguous = __not_found - 1;
28 
29 inline _LIBCPP_HIDE_FROM_ABI constexpr size_t __find_idx_return(size_t __curr_i, size_t __res, bool __matches) {
30   return !__matches ? __res : (__res == __not_found ? __curr_i : __ambiguous);
31 }
32 
33 template <size_t _Nx>
34 inline _LIBCPP_HIDE_FROM_ABI constexpr size_t __find_idx(size_t __i, const bool (&__matches)[_Nx]) {
35   return __i == _Nx
36            ? __not_found
37            : __find_detail::__find_idx_return(__i, __find_detail::__find_idx(__i + 1, __matches), __matches[__i]);
38 }
39 
40 template <class _T1, class... _Args>
41 struct __find_exactly_one_checked {
42   static constexpr bool __matches[sizeof...(_Args)] = {is_same<_T1, _Args>::value...};
43   static constexpr size_t value                     = __find_detail::__find_idx(0, __matches);
44   static_assert(value != __not_found, "type not found in type list");
45   static_assert(value != __ambiguous, "type occurs more than once in type list");
46 };
47 
48 template <class _T1>
49 struct __find_exactly_one_checked<_T1> {
50   static_assert(!is_same<_T1, _T1>::value, "type not in empty type list");
51 };
52 
53 } // namespace __find_detail
54 
55 template <typename _T1, typename... _Args>
56 struct __find_exactly_one_t : public __find_detail::__find_exactly_one_checked<_T1, _Args...> {};
57 
58 _LIBCPP_END_NAMESPACE_STD
59 
60 #endif // _LIBCPP_STD_VER >= 14
61 
62 #endif // _LIBCPP___TUPLE_FIND_INDEX_H
63