File indexing completed on 2025-07-02 08:05:13
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #ifndef ABSL_META_TYPE_TRAITS_H_
0036 #define ABSL_META_TYPE_TRAITS_H_
0037
0038 #include <cstddef>
0039 #include <functional>
0040 #include <string>
0041 #include <type_traits>
0042 #include <vector>
0043
0044 #include "absl/base/attributes.h"
0045 #include "absl/base/config.h"
0046
0047 #ifdef __cpp_lib_span
0048 #include <span> // NOLINT(build/c++20)
0049 #endif
0050
0051 #ifdef ABSL_HAVE_STD_STRING_VIEW
0052 #include <string_view>
0053 #endif
0054
0055
0056
0057 #if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
0058 #define ABSL_INTERNAL_DEFAULT_NEW_ALIGNMENT __STDCPP_DEFAULT_NEW_ALIGNMENT__
0059 #else
0060 #define ABSL_INTERNAL_DEFAULT_NEW_ALIGNMENT alignof(std::max_align_t)
0061 #endif
0062
0063 namespace absl {
0064 ABSL_NAMESPACE_BEGIN
0065
0066 namespace type_traits_internal {
0067
0068 template <typename... Ts>
0069 struct VoidTImpl {
0070 using type = void;
0071 };
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 template <class Enabler, template <class...> class Op, class... Args>
0088 struct is_detected_impl {
0089 using type = std::false_type;
0090 };
0091
0092 template <template <class...> class Op, class... Args>
0093 struct is_detected_impl<typename VoidTImpl<Op<Args...>>::type, Op, Args...> {
0094 using type = std::true_type;
0095 };
0096
0097 template <template <class...> class Op, class... Args>
0098 struct is_detected : is_detected_impl<void, Op, Args...>::type {};
0099
0100 template <class Enabler, class To, template <class...> class Op, class... Args>
0101 struct is_detected_convertible_impl {
0102 using type = std::false_type;
0103 };
0104
0105 template <class To, template <class...> class Op, class... Args>
0106 struct is_detected_convertible_impl<
0107 typename std::enable_if<std::is_convertible<Op<Args...>, To>::value>::type,
0108 To, Op, Args...> {
0109 using type = std::true_type;
0110 };
0111
0112 template <class To, template <class...> class Op, class... Args>
0113 struct is_detected_convertible
0114 : is_detected_convertible_impl<void, To, Op, Args...>::type {};
0115
0116 }
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 template <typename... Ts>
0131 using void_t = typename type_traits_internal::VoidTImpl<Ts...>::type;
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142 template <typename... Ts>
0143 struct conjunction : std::true_type {};
0144
0145 template <typename T, typename... Ts>
0146 struct conjunction<T, Ts...>
0147 : std::conditional<T::value, conjunction<Ts...>, T>::type {};
0148
0149 template <typename T>
0150 struct conjunction<T> : T {};
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 template <typename... Ts>
0162 struct disjunction : std::false_type {};
0163
0164 template <typename T, typename... Ts>
0165 struct disjunction<T, Ts...>
0166 : std::conditional<T::value, T, disjunction<Ts...>>::type {};
0167
0168 template <typename T>
0169 struct disjunction<T> : T {};
0170
0171
0172
0173
0174
0175
0176
0177
0178 template <typename T>
0179 struct negation : std::integral_constant<bool, !T::value> {};
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192 template <typename T>
0193 struct is_function
0194 : std::integral_constant<
0195 bool, !(std::is_reference<T>::value ||
0196 std::is_const<typename std::add_const<T>::type>::value)> {};
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 using std::is_copy_assignable;
0214 using std::is_move_assignable;
0215 using std::is_trivially_copy_assignable;
0216 using std::is_trivially_copy_constructible;
0217 using std::is_trivially_default_constructible;
0218 using std::is_trivially_destructible;
0219 using std::is_trivially_move_assignable;
0220 using std::is_trivially_move_constructible;
0221
0222 #if defined(__cpp_lib_remove_cvref) && __cpp_lib_remove_cvref >= 201711L
0223 template <typename T>
0224 using remove_cvref = std::remove_cvref<T>;
0225
0226 template <typename T>
0227 using remove_cvref_t = typename std::remove_cvref<T>::type;
0228 #else
0229
0230
0231
0232
0233 template <typename T>
0234 struct remove_cvref {
0235 using type =
0236 typename std::remove_cv<typename std::remove_reference<T>::type>::type;
0237 };
0238
0239 template <typename T>
0240 using remove_cvref_t = typename remove_cvref<T>::type;
0241 #endif
0242
0243
0244
0245
0246
0247 template <typename T>
0248 using remove_cv_t = typename std::remove_cv<T>::type;
0249
0250 template <typename T>
0251 using remove_const_t = typename std::remove_const<T>::type;
0252
0253 template <typename T>
0254 using remove_volatile_t = typename std::remove_volatile<T>::type;
0255
0256 template <typename T>
0257 using add_cv_t = typename std::add_cv<T>::type;
0258
0259 template <typename T>
0260 using add_const_t = typename std::add_const<T>::type;
0261
0262 template <typename T>
0263 using add_volatile_t = typename std::add_volatile<T>::type;
0264
0265 template <typename T>
0266 using remove_reference_t = typename std::remove_reference<T>::type;
0267
0268 template <typename T>
0269 using add_lvalue_reference_t = typename std::add_lvalue_reference<T>::type;
0270
0271 template <typename T>
0272 using add_rvalue_reference_t = typename std::add_rvalue_reference<T>::type;
0273
0274 template <typename T>
0275 using remove_pointer_t = typename std::remove_pointer<T>::type;
0276
0277 template <typename T>
0278 using add_pointer_t = typename std::add_pointer<T>::type;
0279
0280 template <typename T>
0281 using make_signed_t = typename std::make_signed<T>::type;
0282
0283 template <typename T>
0284 using make_unsigned_t = typename std::make_unsigned<T>::type;
0285
0286 template <typename T>
0287 using remove_extent_t = typename std::remove_extent<T>::type;
0288
0289 template <typename T>
0290 using remove_all_extents_t = typename std::remove_all_extents<T>::type;
0291
0292 template <typename T>
0293 using decay_t = typename std::decay<T>::type;
0294
0295 template <bool B, typename T = void>
0296 using enable_if_t = typename std::enable_if<B, T>::type;
0297
0298 template <bool B, typename T, typename F>
0299 using conditional_t = typename std::conditional<B, T, F>::type;
0300
0301 template <typename... T>
0302 using common_type_t = typename std::common_type<T...>::type;
0303
0304 template <typename T>
0305 using underlying_type_t = typename std::underlying_type<T>::type;
0306
0307 namespace type_traits_internal {
0308
0309 #if (defined(__cpp_lib_is_invocable) && __cpp_lib_is_invocable >= 201703L) || \
0310 (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
0311
0312 template <typename>
0313 struct result_of;
0314 template <typename F, typename... Args>
0315 struct result_of<F(Args...)> : std::invoke_result<F, Args...> {};
0316 #else
0317 template <typename F>
0318 using result_of = std::result_of<F>;
0319 #endif
0320
0321 }
0322
0323 template <typename F>
0324 using result_of_t = typename type_traits_internal::result_of<F>::type;
0325
0326 namespace type_traits_internal {
0327
0328
0329
0330
0331 #if defined(_MSC_VER) || (defined(_LIBCPP_VERSION) && \
0332 _LIBCPP_VERSION < 4000 && _LIBCPP_STD_VER > 11)
0333 #define ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_ 0
0334 #else
0335 #define ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_ 1
0336 #endif
0337
0338 #if !ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
0339 template <typename Key, typename = size_t>
0340 struct IsHashable : std::true_type {};
0341 #else
0342 template <typename Key, typename = void>
0343 struct IsHashable : std::false_type {};
0344
0345 template <typename Key>
0346 struct IsHashable<
0347 Key,
0348 absl::enable_if_t<std::is_convertible<
0349 decltype(std::declval<std::hash<Key>&>()(std::declval<Key const&>())),
0350 std::size_t>::value>> : std::true_type {};
0351 #endif
0352
0353 struct AssertHashEnabledHelper {
0354 private:
0355 static void Sink(...) {}
0356 struct NAT {};
0357
0358 template <class Key>
0359 static auto GetReturnType(int)
0360 -> decltype(std::declval<std::hash<Key>>()(std::declval<Key const&>()));
0361 template <class Key>
0362 static NAT GetReturnType(...);
0363
0364 template <class Key>
0365 static std::nullptr_t DoIt() {
0366 static_assert(IsHashable<Key>::value,
0367 "std::hash<Key> does not provide a call operator");
0368 static_assert(
0369 std::is_default_constructible<std::hash<Key>>::value,
0370 "std::hash<Key> must be default constructible when it is enabled");
0371 static_assert(
0372 std::is_copy_constructible<std::hash<Key>>::value,
0373 "std::hash<Key> must be copy constructible when it is enabled");
0374 static_assert(absl::is_copy_assignable<std::hash<Key>>::value,
0375 "std::hash<Key> must be copy assignable when it is enabled");
0376
0377
0378 using ReturnType = decltype(GetReturnType<Key>(0));
0379 static_assert(std::is_same<ReturnType, NAT>::value ||
0380 std::is_same<ReturnType, size_t>::value,
0381 "std::hash<Key> must return size_t");
0382 return nullptr;
0383 }
0384
0385 template <class... Ts>
0386 friend void AssertHashEnabled();
0387 };
0388
0389 template <class... Ts>
0390 inline void AssertHashEnabled() {
0391 using Helper = AssertHashEnabledHelper;
0392 Helper::Sink(Helper::DoIt<Ts>()...);
0393 }
0394
0395 }
0396
0397
0398
0399 namespace swap_internal {
0400
0401
0402 using std::swap;
0403
0404
0405
0406 void swap();
0407
0408 template <class T>
0409 using IsSwappableImpl = decltype(swap(std::declval<T&>(), std::declval<T&>()));
0410
0411
0412 template <class T,
0413 class IsNoexcept = std::integral_constant<
0414 bool, noexcept(swap(std::declval<T&>(), std::declval<T&>()))>>
0415 using IsNothrowSwappableImpl = typename std::enable_if<IsNoexcept::value>::type;
0416
0417
0418
0419
0420
0421 template <class T>
0422 struct IsSwappable
0423 : absl::type_traits_internal::is_detected<IsSwappableImpl, T> {};
0424
0425
0426
0427
0428
0429 template <class T>
0430 struct IsNothrowSwappable
0431 : absl::type_traits_internal::is_detected<IsNothrowSwappableImpl, T> {};
0432
0433
0434
0435
0436
0437 template <class T, absl::enable_if_t<IsSwappable<T>::value, int> = 0>
0438 void Swap(T& lhs, T& rhs) noexcept(IsNothrowSwappable<T>::value) {
0439 swap(lhs, rhs);
0440 }
0441
0442
0443
0444
0445
0446
0447 using StdSwapIsUnconstrained = IsSwappable<void()>;
0448
0449 }
0450
0451 namespace type_traits_internal {
0452
0453
0454 using swap_internal::IsNothrowSwappable;
0455 using swap_internal::IsSwappable;
0456 using swap_internal::StdSwapIsUnconstrained;
0457 using swap_internal::Swap;
0458
0459 }
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513 #if ABSL_HAVE_BUILTIN(__is_trivially_relocatable) && \
0514 (defined(__cpp_impl_trivially_relocatable) || \
0515 (!defined(__clang__) && !defined(__APPLE__) && !defined(__NVCC__)))
0516 template <class T>
0517 struct is_trivially_relocatable
0518 : std::integral_constant<bool, __is_trivially_relocatable(T)> {};
0519 #else
0520
0521
0522
0523 template <class T>
0524 struct is_trivially_relocatable : std::is_trivially_copyable<T> {};
0525 #endif
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557 #if defined(ABSL_HAVE_CONSTANT_EVALUATED)
0558 constexpr bool is_constant_evaluated() noexcept {
0559 #ifdef __cpp_lib_is_constant_evaluated
0560 return std::is_constant_evaluated();
0561 #elif ABSL_HAVE_BUILTIN(__builtin_is_constant_evaluated)
0562 return __builtin_is_constant_evaluated();
0563 #endif
0564 }
0565 #endif
0566
0567 namespace type_traits_internal {
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577 template <typename T, typename = void>
0578 struct IsOwnerImpl : std::false_type {
0579 static_assert(std::is_same<T, absl::remove_cvref_t<T>>::value,
0580 "type must lack qualifiers");
0581 };
0582
0583 template <typename T>
0584 struct IsOwnerImpl<
0585 T,
0586 std::enable_if_t<std::is_class<typename T::absl_internal_is_view>::value>>
0587 : absl::negation<typename T::absl_internal_is_view> {};
0588
0589
0590
0591
0592
0593
0594
0595
0596 template <typename T>
0597 struct IsOwner : IsOwnerImpl<T> {};
0598
0599 template <typename T, typename Traits, typename Alloc>
0600 struct IsOwner<std::basic_string<T, Traits, Alloc>> : std::true_type {};
0601
0602 template <typename T, typename Alloc>
0603 struct IsOwner<std::vector<T, Alloc>> : std::true_type {};
0604
0605
0606
0607
0608
0609 template <typename T, typename = void>
0610 struct IsViewImpl : std::false_type {
0611 static_assert(std::is_same<T, absl::remove_cvref_t<T>>::value,
0612 "type must lack qualifiers");
0613 };
0614
0615 template <typename T>
0616 struct IsViewImpl<
0617 T,
0618 std::enable_if_t<std::is_class<typename T::absl_internal_is_view>::value>>
0619 : T::absl_internal_is_view {};
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629 template <typename T>
0630 struct IsView : std::integral_constant<bool, std::is_pointer<T>::value ||
0631 IsViewImpl<T>::value> {};
0632
0633 #ifdef ABSL_HAVE_STD_STRING_VIEW
0634 template <typename Char, typename Traits>
0635 struct IsView<std::basic_string_view<Char, Traits>> : std::true_type {};
0636 #endif
0637
0638 #ifdef __cpp_lib_span
0639 template <typename T>
0640 struct IsView<std::span<T>> : std::true_type {};
0641 #endif
0642
0643
0644
0645
0646
0647
0648
0649
0650 template <typename T, typename U>
0651 using IsLifetimeBoundAssignment =
0652 std::integral_constant<bool, IsView<absl::remove_cvref_t<T>>::value &&
0653 IsOwner<absl::remove_cvref_t<U>>::value>;
0654
0655 }
0656
0657 ABSL_NAMESPACE_END
0658 }
0659
0660 #endif