18 #ifndef OTABSL_META_TYPE_TRAITS_H_
19 #define OTABSL_META_TYPE_TRAITS_H_
23 #include <type_traits>
25 #include "../base/config.h"
29 #if defined(_MSC_VER) && !defined(__clang__) && !defined(__GNUC__)
30 #define OTABSL_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION 1
38 struct is_trivially_destructible;
42 struct is_trivially_move_assignable;
44 namespace type_traits_internal {
47 #if defined(_MSC_VER) && !defined(__GNUC__)
49 #pragma warning(disable : 4624)
50 #endif // defined(_MSC_VER) && !defined(__GNUC__)
53 union SingleMemberUnion {
58 #if defined(_MSC_VER) && !defined(__GNUC__)
60 #endif // defined(_MSC_VER) && !defined(__GNUC__)
63 struct IsTriviallyMoveConstructibleObject
64 : std::integral_constant<
65 bool, std::is_move_constructible<
66 type_traits_internal::SingleMemberUnion<T>>::value &&
67 absl::OTABSL_OPTION_NAMESPACE_NAME::is_trivially_destructible<T>::value> {};
70 struct IsTriviallyCopyConstructibleObject
71 : std::integral_constant<
72 bool, std::is_copy_constructible<
73 type_traits_internal::SingleMemberUnion<T>>::value &&
74 absl::OTABSL_OPTION_NAMESPACE_NAME::is_trivially_destructible<T>::value> {};
80 struct IsTriviallyMoveAssignableReference<T&>
81 : absl::OTABSL_OPTION_NAMESPACE_NAME::is_trivially_move_assignable<T>::type {};
84 struct IsTriviallyMoveAssignableReference<T&&>
85 : absl::OTABSL_OPTION_NAMESPACE_NAME::is_trivially_move_assignable<T>::type {};
87 template <
typename... Ts>
106 template <
class Enabler,
template <
class...>
class Op,
class... Args>
111 template <
template <
class...>
class Op,
class... Args>
116 template <
template <
class...>
class Op,
class... Args>
119 template <
class Enabler,
class To,
template <
class...>
class Op,
class... Args>
120 struct is_detected_convertible_impl {
124 template <
class To,
template <
class...>
class Op,
class... Args>
126 typename
std::enable_if<std::is_convertible<Op<Args...>, To>::value>::type,
131 template <
class To,
template <
class...>
class Op,
class... Args>
135 template <
typename T>
137 decltype(std::declval<T&>() = std::declval<const T&>());
139 template <
typename T>
146 #if defined(_MSC_VER) && !defined(__clang__) && (_MSC_VER >= 1910)
148 template <
typename T>
149 using is_copy_assignable = std::is_copy_assignable<T>;
151 template <
typename T>
152 using is_move_assignable = std::is_move_assignable<T>;
156 template <
typename T>
157 struct is_copy_assignable : type_traits_internal::is_detected<
158 type_traits_internal::IsCopyAssignableImpl, T> {
161 template <
typename T>
162 struct is_move_assignable : type_traits_internal::is_detected<
163 type_traits_internal::IsMoveAssignableImpl, T> {
180 template <
typename... Ts>
181 using void_t =
typename type_traits_internal::VoidTImpl<Ts...>::type;
192 template <
typename... Ts>
195 template <
typename T,
typename... Ts>
196 struct conjunction<T, Ts...>
197 : std::conditional<T::value, conjunction<Ts...>, T>::type {};
199 template <
typename T>
200 struct conjunction<T> : T {};
214 template <
typename... Ts>
217 template <
typename T,
typename... Ts>
218 struct disjunction<T, Ts...> :
219 std::conditional<T::value, T, disjunction<Ts...>>::type {};
221 template <
typename T>
222 struct disjunction<T> : T {};
234 template <
typename T>
235 struct negation : std::integral_constant<bool, !T::value> {};
248 template <
typename T>
250 : std::integral_constant<
251 bool, !(std::is_reference<T>::value ||
252 std::is_const<typename std::add_const<T>::type>::value)> {};
268 template <
typename T>
269 struct is_trivially_destructible
270 #ifdef OTABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
271 : std::is_trivially_destructible<T> {
273 : std::integral_constant<bool, __has_trivial_destructor(T) &&
274 std::is_destructible<T>::value> {
276 #ifdef OTABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
278 static constexpr
bool compliant = std::is_trivially_destructible<T>::value ==
279 is_trivially_destructible::value;
280 static_assert(compliant || std::is_trivially_destructible<T>::value,
281 "Not compliant with std::is_trivially_destructible; "
282 "Standard: false, Implementation: true");
283 static_assert(compliant || !std::is_trivially_destructible<T>::value,
284 "Not compliant with std::is_trivially_destructible; "
285 "Standard: true, Implementation: false");
286 #endif // OTABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
321 template <
typename T>
322 struct is_trivially_default_constructible
323 #if defined(OTABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE)
324 : std::is_trivially_default_constructible<T> {
326 : std::integral_constant<bool, __has_trivial_constructor(T) &&
327 std::is_default_constructible<T>::value &&
328 is_trivially_destructible<T>::value> {
330 #if defined(OTABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE) && \
332 OTABSL_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION)
334 static constexpr
bool compliant =
335 std::is_trivially_default_constructible<T>::value ==
336 is_trivially_default_constructible::value;
337 static_assert(compliant || std::is_trivially_default_constructible<T>::value,
338 "Not compliant with std::is_trivially_default_constructible; "
339 "Standard: false, Implementation: true");
340 static_assert(compliant || !std::is_trivially_default_constructible<T>::value,
341 "Not compliant with std::is_trivially_default_constructible; "
342 "Standard: true, Implementation: false");
343 #endif // OTABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
359 template <
typename T>
360 struct is_trivially_move_constructible
361 #if defined(OTABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE)
362 : std::is_trivially_move_constructible<T> {
365 std::is_object<T>::value && !std::is_array<T>::value,
366 type_traits_internal::IsTriviallyMoveConstructibleObject<T>,
367 std::is_reference<T>>::type::type {
369 #if defined(OTABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE) && \
371 OTABSL_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION)
373 static constexpr
bool compliant =
374 std::is_trivially_move_constructible<T>::value ==
375 is_trivially_move_constructible::value;
376 static_assert(compliant || std::is_trivially_move_constructible<T>::value,
377 "Not compliant with std::is_trivially_move_constructible; "
378 "Standard: false, Implementation: true");
379 static_assert(compliant || !std::is_trivially_move_constructible<T>::value,
380 "Not compliant with std::is_trivially_move_constructible; "
381 "Standard: true, Implementation: false");
382 #endif // OTABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
398 template <
typename T>
399 struct is_trivially_copy_constructible
401 std::is_object<T>::value && !std::is_array<T>::value,
402 type_traits_internal::IsTriviallyCopyConstructibleObject<T>,
403 std::is_lvalue_reference<T>>::type::type {
404 #if defined(OTABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE) && \
406 OTABSL_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION)
408 static constexpr
bool compliant =
409 std::is_trivially_copy_constructible<T>::value ==
410 is_trivially_copy_constructible::value;
411 static_assert(compliant || std::is_trivially_copy_constructible<T>::value,
412 "Not compliant with std::is_trivially_copy_constructible; "
413 "Standard: false, Implementation: true");
414 static_assert(compliant || !std::is_trivially_copy_constructible<T>::value,
415 "Not compliant with std::is_trivially_copy_constructible; "
416 "Standard: true, Implementation: false");
417 #endif // OTABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
435 template <
typename T>
436 struct is_trivially_move_assignable
438 std::is_object<T>::value && !std::is_array<T>::value &&
439 std::is_move_assignable<T>::value,
440 std::is_move_assignable<type_traits_internal::SingleMemberUnion<T>>,
441 type_traits_internal::IsTriviallyMoveAssignableReference<T>>::type::
443 #ifdef OTABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
445 static constexpr
bool compliant =
446 std::is_trivially_move_assignable<T>::value ==
447 is_trivially_move_assignable::value;
448 static_assert(compliant || std::is_trivially_move_assignable<T>::value,
449 "Not compliant with std::is_trivially_move_assignable; "
450 "Standard: false, Implementation: true");
451 static_assert(compliant || !std::is_trivially_move_assignable<T>::value,
452 "Not compliant with std::is_trivially_move_assignable; "
453 "Standard: true, Implementation: false");
454 #endif // OTABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
472 template <
typename T>
473 struct is_trivially_copy_assignable
474 #ifdef OTABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
475 : std::is_trivially_copy_assignable<T> {
477 : std::integral_constant<
478 bool, __has_trivial_assign(
typename std::remove_reference<T>::type) &&
479 absl::OTABSL_OPTION_NAMESPACE_NAME::is_copy_assignable<T>::value> {
481 #ifdef OTABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
483 static constexpr
bool compliant =
484 std::is_trivially_copy_assignable<T>::value ==
485 is_trivially_copy_assignable::value;
486 static_assert(compliant || std::is_trivially_copy_assignable<T>::value,
487 "Not compliant with std::is_trivially_copy_assignable; "
488 "Standard: false, Implementation: true");
489 static_assert(compliant || !std::is_trivially_copy_assignable<T>::value,
490 "Not compliant with std::is_trivially_copy_assignable; "
491 "Standard: true, Implementation: false");
492 #endif // OTABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
495 namespace type_traits_internal {
512 #if defined(OTABSL_HAVE_STD_IS_TRIVIALLY_COPYABLE)
513 template <
typename T>
514 struct is_trivially_copyable : std::is_trivially_copyable<T> {};
516 template <
typename T>
517 class is_trivially_copyable_impl {
518 using ExtentsRemoved =
typename std::remove_all_extents<T>::type;
519 static constexpr
bool kIsCopyOrMoveConstructible =
520 std::is_copy_constructible<ExtentsRemoved>::value ||
521 std::is_move_constructible<ExtentsRemoved>::value;
522 static constexpr
bool kIsCopyOrMoveAssignable =
523 absl::OTABSL_OPTION_NAMESPACE_NAME::is_copy_assignable<ExtentsRemoved>::value ||
524 absl::OTABSL_OPTION_NAMESPACE_NAME::is_move_assignable<ExtentsRemoved>::value;
527 static constexpr
bool kValue =
528 (__has_trivial_copy(ExtentsRemoved) || !kIsCopyOrMoveConstructible) &&
529 (__has_trivial_assign(ExtentsRemoved) || !kIsCopyOrMoveAssignable) &&
530 (kIsCopyOrMoveConstructible || kIsCopyOrMoveAssignable) &&
531 is_trivially_destructible<ExtentsRemoved>::value &&
534 !std::is_reference<ExtentsRemoved>::value;
537 template <
typename T>
538 struct is_trivially_copyable
539 : std::integral_constant<
540 bool, type_traits_internal::is_trivially_copyable_impl<T>::kValue> {};
548 template <
typename T>
549 using remove_cv_t =
typename std::remove_cv<T>::type;
551 template <
typename T>
554 template <
typename T>
557 template <
typename T>
558 using add_cv_t =
typename std::add_cv<T>::type;
560 template <
typename T>
561 using add_const_t =
typename std::add_const<T>::type;
563 template <
typename T>
566 template <
typename T>
569 template <
typename T>
572 template <
typename T>
575 template <
typename T>
578 template <
typename T>
581 template <
typename T>
584 template <
typename T>
587 template <
typename T>
590 template <
typename T>
593 template <
typename T>
594 using decay_t =
typename std::decay<T>::type;
596 template <
bool B,
typename T =
void>
597 using enable_if_t =
typename std::enable_if<B, T>::type;
599 template <
bool B,
typename T,
typename F>
600 using conditional_t =
typename std::conditional<B, T, F>::type;
602 template <
typename... T>
605 template <
typename T>
608 namespace type_traits_internal {
610 #if (!defined(_MSVC_LANG) && (__cplusplus >= 201703L)) || \
611 (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))
614 template<
typename F,
typename... Args>
617 template<
typename F>
using result_of = std::result_of<F>;
623 using result_of_t =
typename type_traits_internal::result_of<F>::type;
625 namespace type_traits_internal {
630 #if defined(_MSC_VER) || (defined(_LIBCPP_VERSION) && \
631 _LIBCPP_VERSION < 4000 && _LIBCPP_STD_VER > 11)
632 #define OTABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_ 0
634 #define OTABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_ 1
637 #if !OTABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
638 template <
typename Key,
typename =
size_t>
640 #else // OTABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
641 template <
typename Key,
typename =
void>
644 template <
typename Key>
648 decltype(std::declval<std::hash<Key>&>()(std::declval<Key const&>())),
650 #endif // !OTABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
652 struct AssertHashEnabledHelper {
654 static void Sink(...) {}
658 static auto GetReturnType(
int)
659 -> decltype(std::declval<std::hash<Key>>()(std::declval<Key const&>()));
661 static NAT GetReturnType(...);
664 static std::nullptr_t DoIt() {
665 static_assert(IsHashable<Key>::value,
666 "std::hash<Key> does not provide a call operator");
668 std::is_default_constructible<std::hash<Key>>::value,
669 "std::hash<Key> must be default constructible when it is enabled");
671 std::is_copy_constructible<std::hash<Key>>::value,
672 "std::hash<Key> must be copy constructible when it is enabled");
673 static_assert(absl::OTABSL_OPTION_NAMESPACE_NAME::is_copy_assignable<std::hash<Key>>::value,
674 "std::hash<Key> must be copy assignable when it is enabled");
677 using ReturnType = decltype(GetReturnType<Key>(0));
678 static_assert(std::is_same<ReturnType, NAT>::value ||
679 std::is_same<ReturnType, size_t>::value,
680 "std::hash<Key> must return size_t");
684 template <
class... Ts>
688 template <
class... Ts>
690 using Helper = AssertHashEnabledHelper;
691 Helper::Sink(Helper::DoIt<Ts>()...);
698 namespace swap_internal {
712 class IsNoexcept = std::integral_constant<
713 bool, noexcept(
swap(std::declval<T&>(), std::declval<T&>()))>>
722 : absl::OTABSL_OPTION_NAMESPACE_NAME::type_traits_internal::is_detected<IsSwappableImpl, T> {};
729 struct IsNothrowSwappable
730 : absl::OTABSL_OPTION_NAMESPACE_NAME::type_traits_internal::is_detected<IsNothrowSwappableImpl, T> {};
736 template <class T, absl::OTABSL_OPTION_NAMESPACE_NAME::enable_if_t<IsSwappable<T>::value,
int> = 0>
737 void Swap(T& lhs, T& rhs) noexcept(IsNothrowSwappable<T>::value) {
750 namespace type_traits_internal {
753 using swap_internal::IsNothrowSwappable;
754 using swap_internal::IsSwappable;
762 #endif // OTABSL_META_TYPE_TRAITS_H_