From 7b46123329a756ea2ba59fde56af5f7c214364a3 Mon Sep 17 00:00:00 2001 From: Derek Mauro Date: Mon, 27 Feb 2023 07:32:56 -0800 Subject: Remove backfills of some type traits that are now fully supported PiperOrigin-RevId: 512622121 Change-Id: If90aaf7939062bd475253c372d9d7950c98c7150 --- absl/meta/type_traits.h | 386 ++----------------------- absl/meta/type_traits_test.cc | 655 ------------------------------------------ 2 files changed, 21 insertions(+), 1020 deletions(-) (limited to 'absl/meta') diff --git a/absl/meta/type_traits.h b/absl/meta/type_traits.h index b1656c39..fb12ba4a 100644 --- a/absl/meta/type_traits.h +++ b/absl/meta/type_traits.h @@ -41,12 +41,6 @@ #include "absl/base/config.h" -// MSVC constructibility traits do not detect destructor properties and so our -// implementations should not use them as a source-of-truth. -#if defined(_MSC_VER) && !defined(__clang__) && !defined(__GNUC__) -#define ABSL_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION 1 -#endif - // Defines the default alignment. `__STDCPP_DEFAULT_NEW_ALIGNMENT__` is a C++17 // feature. #if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__) @@ -58,57 +52,8 @@ namespace absl { ABSL_NAMESPACE_BEGIN -// Defined and documented later on in this file. -template -struct is_trivially_destructible; - -// Defined and documented later on in this file. -template -struct is_trivially_move_assignable; - namespace type_traits_internal { -// Silence MSVC warnings about the destructor being defined as deleted. -#if defined(_MSC_VER) && !defined(__GNUC__) -#pragma warning(push) -#pragma warning(disable : 4624) -#endif // defined(_MSC_VER) && !defined(__GNUC__) - -template -union SingleMemberUnion { - T t; -}; - -// Restore the state of the destructor warning that was silenced above. -#if defined(_MSC_VER) && !defined(__GNUC__) -#pragma warning(pop) -#endif // defined(_MSC_VER) && !defined(__GNUC__) - -template -struct IsTriviallyMoveConstructibleObject - : std::integral_constant< - bool, std::is_move_constructible< - type_traits_internal::SingleMemberUnion>::value && - absl::is_trivially_destructible::value> {}; - -template -struct IsTriviallyCopyConstructibleObject - : std::integral_constant< - bool, std::is_copy_constructible< - type_traits_internal::SingleMemberUnion>::value && - absl::is_trivially_destructible::value> {}; - -template -struct IsTriviallyMoveAssignableReference : std::false_type {}; - -template -struct IsTriviallyMoveAssignableReference - : absl::is_trivially_move_assignable::type {}; - -template -struct IsTriviallyMoveAssignableReference - : absl::is_trivially_move_assignable::type {}; - template struct VoidTImpl { using type = void; @@ -157,39 +102,8 @@ template class Op, class... Args> struct is_detected_convertible : is_detected_convertible_impl::type {}; -template -using IsCopyAssignableImpl = - decltype(std::declval() = std::declval()); - -template -using IsMoveAssignableImpl = decltype(std::declval() = std::declval()); - } // namespace type_traits_internal -// MSVC 19.20 has a regression that causes our workarounds to fail, but their -// std forms now appear to be compliant. -#if defined(_MSC_VER) && !defined(__clang__) && (_MSC_VER >= 1920) - -template -using is_copy_assignable = std::is_copy_assignable; - -template -using is_move_assignable = std::is_move_assignable; - -#else - -template -struct is_copy_assignable : type_traits_internal::is_detected< - type_traits_internal::IsCopyAssignableImpl, T> { -}; - -template -struct is_move_assignable : type_traits_internal::is_detected< - type_traits_internal::IsMoveAssignableImpl, T> { -}; - -#endif - // void_t() // // Ignores the type of any its arguments and returns `void`. In general, this @@ -270,246 +184,29 @@ struct is_function bool, !(std::is_reference::value || std::is_const::type>::value)> {}; +// is_copy_assignable() +// is_move_assignable() // is_trivially_destructible() -// -// Determines whether the passed type `T` is trivially destructible. -// -// This metafunction is designed to be a drop-in replacement for the C++11 -// `std::is_trivially_destructible()` metafunction for platforms that have -// incomplete C++11 support (such as libstdc++ 4.x). On any platforms that do -// fully support C++11, we check whether this yields the same result as the std -// implementation. -// -// NOTE: the extensions (__has_trivial_xxx) are implemented in gcc (version >= -// 4.3) and clang. Since we are supporting libstdc++ > 4.7, they should always -// be present. These extensions are documented at -// https://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html#Type-Traits. -template -struct is_trivially_destructible -#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE - : std::is_trivially_destructible { -#else - : std::integral_constant::value> { -#endif -#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE - private: - static constexpr bool compliant = std::is_trivially_destructible::value == - is_trivially_destructible::value; - static_assert(compliant || std::is_trivially_destructible::value, - "Not compliant with std::is_trivially_destructible; " - "Standard: false, Implementation: true"); - static_assert(compliant || !std::is_trivially_destructible::value, - "Not compliant with std::is_trivially_destructible; " - "Standard: true, Implementation: false"); -#endif // ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE -}; - // is_trivially_default_constructible() -// -// Determines whether the passed type `T` is trivially default constructible. -// -// This metafunction is designed to be a drop-in replacement for the C++11 -// `std::is_trivially_default_constructible()` metafunction for platforms that -// have incomplete C++11 support (such as libstdc++ 4.x). On any platforms that -// do fully support C++11, we check whether this yields the same result as the -// std implementation. -// -// NOTE: according to the C++ standard, Section: 20.15.4.3 [meta.unary.prop] -// "The predicate condition for a template specialization is_constructible shall be satisfied if and only if the following variable -// definition would be well-formed for some invented variable t: -// -// T t(declval()...); -// -// is_trivially_constructible additionally requires that the -// variable definition does not call any operation that is not trivial. -// For the purposes of this check, the call to std::declval is considered -// trivial." -// -// Notes from https://en.cppreference.com/w/cpp/types/is_constructible: -// In many implementations, is_nothrow_constructible also checks if the -// destructor throws because it is effectively noexcept(T(arg)). Same -// applies to is_trivially_constructible, which, in these implementations, also -// requires that the destructor is trivial. -// GCC bug 51452: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452 -// LWG issue 2116: http://cplusplus.github.io/LWG/lwg-active.html#2116. -// -// "T obj();" need to be well-formed and not call any nontrivial operation. -// Nontrivially destructible types will cause the expression to be nontrivial. -template -struct is_trivially_default_constructible -#if defined(ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE) - : std::is_trivially_default_constructible { -#else - : std::integral_constant::value && - is_trivially_destructible::value> { -#endif -#if defined(ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE) && \ - !defined( \ - ABSL_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION) - private: - static constexpr bool compliant = - std::is_trivially_default_constructible::value == - is_trivially_default_constructible::value; - static_assert(compliant || std::is_trivially_default_constructible::value, - "Not compliant with std::is_trivially_default_constructible; " - "Standard: false, Implementation: true"); - static_assert(compliant || !std::is_trivially_default_constructible::value, - "Not compliant with std::is_trivially_default_constructible; " - "Standard: true, Implementation: false"); -#endif // ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE -}; - // is_trivially_move_constructible() -// -// Determines whether the passed type `T` is trivially move constructible. -// -// This metafunction is designed to be a drop-in replacement for the C++11 -// `std::is_trivially_move_constructible()` metafunction for platforms that have -// incomplete C++11 support (such as libstdc++ 4.x). On any platforms that do -// fully support C++11, we check whether this yields the same result as the std -// implementation. -// -// NOTE: `T obj(declval());` needs to be well-formed and not call any -// nontrivial operation. Nontrivially destructible types will cause the -// expression to be nontrivial. -template -struct is_trivially_move_constructible -#if defined(ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE) - : std::is_trivially_move_constructible { -#else - : std::conditional< - std::is_object::value && !std::is_array::value, - type_traits_internal::IsTriviallyMoveConstructibleObject, - std::is_reference>::type::type { -#endif -#if defined(ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE) && \ - !defined( \ - ABSL_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION) - private: - static constexpr bool compliant = - std::is_trivially_move_constructible::value == - is_trivially_move_constructible::value; - static_assert(compliant || std::is_trivially_move_constructible::value, - "Not compliant with std::is_trivially_move_constructible; " - "Standard: false, Implementation: true"); - static_assert(compliant || !std::is_trivially_move_constructible::value, - "Not compliant with std::is_trivially_move_constructible; " - "Standard: true, Implementation: false"); -#endif // ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE -}; - // is_trivially_copy_constructible() -// -// Determines whether the passed type `T` is trivially copy constructible. -// -// This metafunction is designed to be a drop-in replacement for the C++11 -// `std::is_trivially_copy_constructible()` metafunction for platforms that have -// incomplete C++11 support (such as libstdc++ 4.x). On any platforms that do -// fully support C++11, we check whether this yields the same result as the std -// implementation. -// -// NOTE: `T obj(declval());` needs to be well-formed and not call any -// nontrivial operation. Nontrivially destructible types will cause the -// expression to be nontrivial. -template -struct is_trivially_copy_constructible - : std::conditional< - std::is_object::value && !std::is_array::value, - type_traits_internal::IsTriviallyCopyConstructibleObject, - std::is_lvalue_reference>::type::type { -#if defined(ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE) && \ - !defined( \ - ABSL_META_INTERNAL_STD_CONSTRUCTION_TRAITS_DONT_CHECK_DESTRUCTION) - private: - static constexpr bool compliant = - std::is_trivially_copy_constructible::value == - is_trivially_copy_constructible::value; - static_assert(compliant || std::is_trivially_copy_constructible::value, - "Not compliant with std::is_trivially_copy_constructible; " - "Standard: false, Implementation: true"); - static_assert(compliant || !std::is_trivially_copy_constructible::value, - "Not compliant with std::is_trivially_copy_constructible; " - "Standard: true, Implementation: false"); -#endif // ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE -}; - // is_trivially_move_assignable() -// -// Determines whether the passed type `T` is trivially move assignable. -// -// This metafunction is designed to be a drop-in replacement for the C++11 -// `std::is_trivially_move_assignable()` metafunction for platforms that have -// incomplete C++11 support (such as libstdc++ 4.x). On any platforms that do -// fully support C++11, we check whether this yields the same result as the std -// implementation. -// -// NOTE: `is_assignable::value` is `true` if the expression -// `declval() = declval()` is well-formed when treated as an unevaluated -// operand. `is_trivially_assignable` requires the assignment to call no -// operation that is not trivial. `is_trivially_copy_assignable` is simply -// `is_trivially_assignable`. -template -struct is_trivially_move_assignable - : std::conditional< - std::is_object::value && !std::is_array::value && - std::is_move_assignable::value, - std::is_move_assignable>, - type_traits_internal::IsTriviallyMoveAssignableReference>::type:: - type { -#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE - private: - static constexpr bool compliant = - std::is_trivially_move_assignable::value == - is_trivially_move_assignable::value; - static_assert(compliant || std::is_trivially_move_assignable::value, - "Not compliant with std::is_trivially_move_assignable; " - "Standard: false, Implementation: true"); - static_assert(compliant || !std::is_trivially_move_assignable::value, - "Not compliant with std::is_trivially_move_assignable; " - "Standard: true, Implementation: false"); -#endif // ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE -}; - // is_trivially_copy_assignable() // -// Determines whether the passed type `T` is trivially copy assignable. -// -// This metafunction is designed to be a drop-in replacement for the C++11 -// `std::is_trivially_copy_assignable()` metafunction for platforms that have -// incomplete C++11 support (such as libstdc++ 4.x). On any platforms that do -// fully support C++11, we check whether this yields the same result as the std -// implementation. -// -// NOTE: `is_assignable::value` is `true` if the expression -// `declval() = declval()` is well-formed when treated as an unevaluated -// operand. `is_trivially_assignable` requires the assignment to call no -// operation that is not trivial. `is_trivially_copy_assignable` is simply -// `is_trivially_assignable`. -template -struct is_trivially_copy_assignable -#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE - : std::is_trivially_copy_assignable { -#else - : std::integral_constant< - bool, __has_trivial_assign(typename std::remove_reference::type) && - absl::is_copy_assignable::value> { -#endif -#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE - private: - static constexpr bool compliant = - std::is_trivially_copy_assignable::value == - is_trivially_copy_assignable::value; - static_assert(compliant || std::is_trivially_copy_assignable::value, - "Not compliant with std::is_trivially_copy_assignable; " - "Standard: false, Implementation: true"); - static_assert(compliant || !std::is_trivially_copy_assignable::value, - "Not compliant with std::is_trivially_copy_assignable; " - "Standard: true, Implementation: false"); -#endif // ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE -}; +// Historical note: Abseil once provided implementations of these type traits +// for platforms that lacked full support. New code should prefer to use the +// std variants. +// +// See the documentation for the STL header for more information: +// https://en.cppreference.com/w/cpp/header/type_traits +using std::is_copy_assignable; +using std::is_move_assignable; +using std::is_trivially_copy_assignable; +using std::is_trivially_copy_constructible; +using std::is_trivially_default_constructible; +using std::is_trivially_destructible; +using std::is_trivially_move_assignable; +using std::is_trivially_move_constructible; #if defined(__cpp_lib_remove_cvref) && __cpp_lib_remove_cvref >= 201711L template @@ -533,52 +230,11 @@ using remove_cvref_t = typename remove_cvref::type; #endif namespace type_traits_internal { -// is_trivially_copyable() -// -// Determines whether the passed type `T` is trivially copyable. -// -// This metafunction is designed to be a drop-in replacement for the C++11 -// `std::is_trivially_copyable()` metafunction for platforms that have -// incomplete C++11 support (such as libstdc++ 4.x). We use the C++17 definition -// of TriviallyCopyable. -// -// NOTE: `is_trivially_copyable::value` is `true` if all of T's copy/move -// constructors/assignment operators are trivial or deleted, T has at least -// one non-deleted copy/move constructor/assignment operator, and T is trivially -// destructible. Arrays of trivially copyable types are trivially copyable. -// -// We expose this metafunction only for internal use within absl. - -#if defined(ABSL_HAVE_STD_IS_TRIVIALLY_COPYABLE) -template -struct is_trivially_copyable : std::is_trivially_copyable {}; -#else -template -class is_trivially_copyable_impl { - using ExtentsRemoved = typename std::remove_all_extents::type; - static constexpr bool kIsCopyOrMoveConstructible = - std::is_copy_constructible::value || - std::is_move_constructible::value; - static constexpr bool kIsCopyOrMoveAssignable = - absl::is_copy_assignable::value || - absl::is_move_assignable::value; - - public: - static constexpr bool kValue = - (__has_trivial_copy(ExtentsRemoved) || !kIsCopyOrMoveConstructible) && - (__has_trivial_assign(ExtentsRemoved) || !kIsCopyOrMoveAssignable) && - (kIsCopyOrMoveConstructible || kIsCopyOrMoveAssignable) && - is_trivially_destructible::value && - // We need to check for this explicitly because otherwise we'll say - // references are trivial copyable when compiled by MSVC. - !std::is_reference::value; -}; - -template -struct is_trivially_copyable - : std::integral_constant< - bool, type_traits_internal::is_trivially_copyable_impl::kValue> {}; -#endif +// An implementation of std::is_trivially_copyable was once provided for +// internal use within absl. +// TODO(absl-team): Replace absl::type_traits_internal::is_trivially_copyable +// with std::is_trivially_copyable and delete this using declaration. +using std::is_trivially_copyable; } // namespace type_traits_internal // ----------------------------------------------------------------------------- diff --git a/absl/meta/type_traits_test.cc b/absl/meta/type_traits_test.cc index b2a7a67b..4165fef5 100644 --- a/absl/meta/type_traits_test.cc +++ b/absl/meta/type_traits_test.cc @@ -352,29 +352,6 @@ class Base { virtual ~Base() {} }; -// Old versions of libc++, around Clang 3.5 to 3.6, consider deleted destructors -// as also being trivial. With the resolution of CWG 1928 and CWG 1734, this -// is no longer considered true and has thus been amended. -// Compiler Explorer: https://godbolt.org/g/zT59ZL -// CWG issue 1734: http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1734 -// CWG issue 1928: http://open-std.org/JTC1/SC22/WG21/docs/cwg_closed.html#1928 -#if !defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 3700 -#define ABSL_TRIVIALLY_DESTRUCTIBLE_CONSIDER_DELETED_DESTRUCTOR_NOT_TRIVIAL 1 -#endif - -// As of the moment, GCC versions >5.1 have a problem compiling for -// std::is_trivially_default_constructible, where -// NontrivialDestructor is a struct with a custom nontrivial destructor. Note -// that this problem only occurs for arrays of a known size, so something like -// std::is_trivially_default_constructible does not -// have any problems. -// Compiler Explorer: https://godbolt.org/g/dXRbdK -// GCC bug 83689: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83689 -#if defined(__clang__) || defined(_MSC_VER) || \ - (defined(__GNUC__) && __GNUC__ < 5) -#define ABSL_GCC_BUG_TRIVIALLY_CONSTRUCTIBLE_ON_ARRAY_OF_NONTRIVIAL 1 -#endif - TEST(TypeTraitsTest, TestIsFunction) { struct Callable { void operator()() {} @@ -391,562 +368,6 @@ TEST(TypeTraitsTest, TestIsFunction) { EXPECT_FALSE(absl::is_function::value); } -TEST(TypeTraitsTest, TestTrivialDestructor) { - // Verify that arithmetic types and pointers have trivial destructors. - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - - // classes with destructors - EXPECT_TRUE(absl::is_trivially_destructible::value); - EXPECT_TRUE(absl::is_trivially_destructible::value); - - // Verify that types with a nontrivial or deleted destructor - // are marked as such. - EXPECT_FALSE(absl::is_trivially_destructible::value); -#ifdef ABSL_TRIVIALLY_DESTRUCTIBLE_CONSIDER_DELETED_DESTRUCTOR_NOT_TRIVIAL - EXPECT_FALSE(absl::is_trivially_destructible::value); -#endif - - // simple_pair of such types is trivial - EXPECT_TRUE((absl::is_trivially_destructible>::value)); - EXPECT_TRUE((absl::is_trivially_destructible< - simple_pair>::value)); - - // Verify that types without trivial destructors are correctly marked as such. - EXPECT_FALSE(absl::is_trivially_destructible::value); - EXPECT_FALSE(absl::is_trivially_destructible>::value); - - // Verify that simple_pairs of types without trivial destructors - // are not marked as trivial. - EXPECT_FALSE((absl::is_trivially_destructible< - simple_pair>::value)); - EXPECT_FALSE((absl::is_trivially_destructible< - simple_pair>::value)); - - // array of such types is trivial - using int10 = int[10]; - EXPECT_TRUE(absl::is_trivially_destructible::value); - using Trivial10 = Trivial[10]; - EXPECT_TRUE(absl::is_trivially_destructible::value); - using TrivialDestructor10 = TrivialDestructor[10]; - EXPECT_TRUE(absl::is_trivially_destructible::value); - - // Conversely, the opposite also holds. - using NontrivialDestructor10 = NontrivialDestructor[10]; - EXPECT_FALSE(absl::is_trivially_destructible::value); -} - -TEST(TypeTraitsTest, TestTrivialDefaultCtor) { - // arithmetic types and pointers have trivial default constructors. - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE( - absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - - // types with compiler generated default ctors - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - EXPECT_TRUE( - absl::is_trivially_default_constructible::value); - - // Verify that types without them are not. - EXPECT_FALSE( - absl::is_trivially_default_constructible::value); - EXPECT_FALSE( - absl::is_trivially_default_constructible::value); - - // types with nontrivial destructor are nontrivial - EXPECT_FALSE( - absl::is_trivially_default_constructible::value); - - // types with vtables - EXPECT_FALSE(absl::is_trivially_default_constructible::value); - - // Verify that simple_pair has trivial constructors where applicable. - EXPECT_TRUE((absl::is_trivially_default_constructible< - simple_pair>::value)); - EXPECT_TRUE((absl::is_trivially_default_constructible< - simple_pair>::value)); - EXPECT_TRUE((absl::is_trivially_default_constructible< - simple_pair>::value)); - - // Verify that types without trivial constructors are - // correctly marked as such. - EXPECT_FALSE(absl::is_trivially_default_constructible::value); - EXPECT_FALSE( - absl::is_trivially_default_constructible>::value); - - // Verify that simple_pairs of types without trivial constructors - // are not marked as trivial. - EXPECT_FALSE((absl::is_trivially_default_constructible< - simple_pair>::value)); - EXPECT_FALSE((absl::is_trivially_default_constructible< - simple_pair>::value)); - - // Verify that arrays of such types are trivially default constructible - using int10 = int[10]; - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - using Trivial10 = Trivial[10]; - EXPECT_TRUE(absl::is_trivially_default_constructible::value); - using TrivialDefaultCtor10 = TrivialDefaultCtor[10]; - EXPECT_TRUE( - absl::is_trivially_default_constructible::value); - - // Conversely, the opposite also holds. -#ifdef ABSL_GCC_BUG_TRIVIALLY_CONSTRUCTIBLE_ON_ARRAY_OF_NONTRIVIAL - using NontrivialDefaultCtor10 = NontrivialDefaultCtor[10]; - EXPECT_FALSE( - absl::is_trivially_default_constructible::value); -#endif -} - -// GCC prior to 7.4 had a bug in its trivially-constructible traits -// (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654). -// This test makes sure that we do not depend on the trait in these cases when -// implementing absl triviality traits. - -template -struct BadConstructors { - BadConstructors() { static_assert(T::value, ""); } - - BadConstructors(BadConstructors&&) { static_assert(T::value, ""); } - - BadConstructors(const BadConstructors&) { static_assert(T::value, ""); } -}; - -TEST(TypeTraitsTest, TestTrivialityBadConstructors) { - using BadType = BadConstructors; - - EXPECT_FALSE(absl::is_trivially_default_constructible::value); - EXPECT_FALSE(absl::is_trivially_move_constructible::value); - EXPECT_FALSE(absl::is_trivially_copy_constructible::value); -} - -TEST(TypeTraitsTest, TestTrivialMoveCtor) { - // Verify that arithmetic types and pointers have trivial move - // constructors. - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - - // Reference types - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - - // types with compiler generated move ctors - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - EXPECT_TRUE(absl::is_trivially_move_constructible::value); - - // Verify that types without them (i.e. nontrivial or deleted) are not. - EXPECT_FALSE( - absl::is_trivially_move_constructible::value); - EXPECT_FALSE(absl::is_trivially_move_constructible::value); - EXPECT_FALSE( - absl::is_trivially_move_constructible::value); - - // type with nontrivial destructor are nontrivial move construbtible - EXPECT_FALSE( - absl::is_trivially_move_constructible::value); - - // types with vtables - EXPECT_FALSE(absl::is_trivially_move_constructible::value); - - // Verify that simple_pair of such types is trivially move constructible - EXPECT_TRUE( - (absl::is_trivially_move_constructible>::value)); - EXPECT_TRUE(( - absl::is_trivially_move_constructible>::value)); - EXPECT_TRUE((absl::is_trivially_move_constructible< - simple_pair>::value)); - - // Verify that types without trivial move constructors are - // correctly marked as such. - EXPECT_FALSE(absl::is_trivially_move_constructible::value); - EXPECT_FALSE(absl::is_trivially_move_constructible>::value); - - // Verify that simple_pairs of types without trivial move constructors - // are not marked as trivial. - EXPECT_FALSE((absl::is_trivially_move_constructible< - simple_pair>::value)); - EXPECT_FALSE((absl::is_trivially_move_constructible< - simple_pair>::value)); - - // Verify that arrays are not - using int10 = int[10]; - EXPECT_FALSE(absl::is_trivially_move_constructible::value); -} - -TEST(TypeTraitsTest, TestTrivialCopyCtor) { - // Verify that arithmetic types and pointers have trivial copy - // constructors. - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - - // Reference types - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_FALSE(absl::is_trivially_copy_constructible::value); - - // types with compiler generated copy ctors - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - EXPECT_TRUE(absl::is_trivially_copy_constructible::value); - - // Verify that types without them (i.e. nontrivial or deleted) are not. - EXPECT_FALSE( - absl::is_trivially_copy_constructible::value); - EXPECT_FALSE(absl::is_trivially_copy_constructible::value); - EXPECT_FALSE( - absl::is_trivially_copy_constructible::value); - EXPECT_FALSE( - absl::is_trivially_copy_constructible::value); - - // type with nontrivial destructor are nontrivial copy construbtible - EXPECT_FALSE( - absl::is_trivially_copy_constructible::value); - - // types with vtables - EXPECT_FALSE(absl::is_trivially_copy_constructible::value); - - // Verify that simple_pair of such types is trivially copy constructible - EXPECT_TRUE( - (absl::is_trivially_copy_constructible>::value)); - EXPECT_TRUE(( - absl::is_trivially_copy_constructible>::value)); - EXPECT_TRUE((absl::is_trivially_copy_constructible< - simple_pair>::value)); - - // Verify that types without trivial copy constructors are - // correctly marked as such. - EXPECT_FALSE(absl::is_trivially_copy_constructible::value); - EXPECT_FALSE(absl::is_trivially_copy_constructible>::value); - - // Verify that simple_pairs of types without trivial copy constructors - // are not marked as trivial. - EXPECT_FALSE((absl::is_trivially_copy_constructible< - simple_pair>::value)); - EXPECT_FALSE((absl::is_trivially_copy_constructible< - simple_pair>::value)); - - // Verify that arrays are not - using int10 = int[10]; - EXPECT_FALSE(absl::is_trivially_copy_constructible::value); -} - -TEST(TypeTraitsTest, TestTrivialMoveAssign) { - // Verify that arithmetic types and pointers have trivial move - // assignment operators. - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - - // const qualified types are not assignable - EXPECT_FALSE(absl::is_trivially_move_assignable::value); - - // types with compiler generated move assignment - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - - // Verify that types without them (i.e. nontrivial or deleted) are not. - EXPECT_FALSE(absl::is_trivially_move_assignable::value); - EXPECT_FALSE(absl::is_trivially_move_assignable::value); - EXPECT_FALSE(absl::is_trivially_move_assignable::value); - - // types with vtables - EXPECT_FALSE(absl::is_trivially_move_assignable::value); - - // Verify that simple_pair is trivially assignable - EXPECT_TRUE( - (absl::is_trivially_move_assignable>::value)); - EXPECT_TRUE( - (absl::is_trivially_move_assignable>::value)); - EXPECT_TRUE((absl::is_trivially_move_assignable< - simple_pair>::value)); - - // Verify that types not trivially move assignable are - // correctly marked as such. - EXPECT_FALSE(absl::is_trivially_move_assignable::value); - EXPECT_FALSE(absl::is_trivially_move_assignable>::value); - - // Verify that simple_pairs of types not trivially move assignable - // are not marked as trivial. - EXPECT_FALSE((absl::is_trivially_move_assignable< - simple_pair>::value)); - EXPECT_FALSE((absl::is_trivially_move_assignable< - simple_pair>::value)); - - // Verify that arrays are not trivially move assignable - using int10 = int[10]; - EXPECT_FALSE(absl::is_trivially_move_assignable::value); - - // Verify that references are handled correctly - EXPECT_TRUE(absl::is_trivially_move_assignable::value); - EXPECT_TRUE(absl::is_trivially_move_assignable::value); -} - -TEST(TypeTraitsTest, TestTrivialCopyAssign) { - // Verify that arithmetic types and pointers have trivial copy - // assignment operators. - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - - // const qualified types are not assignable - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - - // types with compiler generated copy assignment - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - - // Verify that types without them (i.e. nontrivial or deleted) are not. - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - - // types with vtables - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - - // Verify that simple_pair is trivially assignable - EXPECT_TRUE( - (absl::is_trivially_copy_assignable>::value)); - EXPECT_TRUE( - (absl::is_trivially_copy_assignable>::value)); - EXPECT_TRUE((absl::is_trivially_copy_assignable< - simple_pair>::value)); - - // Verify that types not trivially copy assignable are - // correctly marked as such. - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - EXPECT_FALSE(absl::is_trivially_copy_assignable>::value); - - // Verify that simple_pairs of types not trivially copy assignable - // are not marked as trivial. - EXPECT_FALSE((absl::is_trivially_copy_assignable< - simple_pair>::value)); - EXPECT_FALSE((absl::is_trivially_copy_assignable< - simple_pair>::value)); - - // Verify that arrays are not trivially copy assignable - using int10 = int[10]; - EXPECT_FALSE(absl::is_trivially_copy_assignable::value); - - // Verify that references are handled correctly - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); - EXPECT_TRUE(absl::is_trivially_copy_assignable::value); -} - -TEST(TypeTraitsTest, TestTriviallyCopyable) { - // Verify that arithmetic types and pointers are trivially copyable. - EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable< - const std::string*>::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - - // const qualified types are not assignable but are constructible - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - - // Trivial copy constructor/assignment and destructor. - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - // Trivial copy assignment, but non-trivial copy constructor/destructor. - EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable< - TrivialCopyAssign>::value); - // Trivial copy constructor, but non-trivial assignment. - EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable< - TrivialCopyCtor>::value); - - // Types with a non-trivial copy constructor/assignment - EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable< - NontrivialCopyCtor>::value); - EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable< - NontrivialCopyAssign>::value); - - // Types without copy constructor/assignment, but with move - // MSVC disagrees with other compilers about this: - // EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable< - // MovableNonCopyable>::value); - - // Types without copy/move constructor/assignment - EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable< - NonCopyableOrMovable>::value); - - // No copy assign, but has trivial copy constructor. - EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable< - DeletedCopyAssign>::value); - - // types with vtables - EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable::value); - - // Verify that simple_pair is trivially copyable if members are - EXPECT_TRUE((absl::type_traits_internal::is_trivially_copyable< - simple_pair>::value)); - EXPECT_TRUE((absl::type_traits_internal::is_trivially_copyable< - simple_pair>::value)); - - // Verify that types not trivially copyable are - // correctly marked as such. - EXPECT_FALSE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_FALSE(absl::type_traits_internal::is_trivially_copyable< - std::vector>::value); - - // Verify that simple_pairs of types not trivially copyable - // are not marked as trivial. - EXPECT_FALSE((absl::type_traits_internal::is_trivially_copyable< - simple_pair>::value)); - EXPECT_FALSE((absl::type_traits_internal::is_trivially_copyable< - simple_pair>::value)); - EXPECT_FALSE((absl::type_traits_internal::is_trivially_copyable< - simple_pair>::value)); - - // Verify that arrays of trivially copyable types are trivially copyable - using int10 = int[10]; - EXPECT_TRUE(absl::type_traits_internal::is_trivially_copyable::value); - using int10x10 = int[10][10]; - EXPECT_TRUE( - absl::type_traits_internal::is_trivially_copyable::value); - - // Verify that references are handled correctly - EXPECT_FALSE( - absl::type_traits_internal::is_trivially_copyable::value); - EXPECT_FALSE( - absl::type_traits_internal::is_trivially_copyable::value); -} - TEST(TypeTraitsTest, TestRemoveCVRef) { EXPECT_TRUE( (std::is_same::type, int>::value)); @@ -1241,82 +662,6 @@ TEST(TypeTraitsTest, TestResultOf) { EXPECT_EQ(TypeEnum::D, GetTypeExt(Wrap())); } -template -bool TestCopyAssign() { - return absl::is_copy_assignable::value == - std::is_copy_assignable::value; -} - -TEST(TypeTraitsTest, IsCopyAssignable) { - EXPECT_TRUE(TestCopyAssign()); - EXPECT_TRUE(TestCopyAssign()); - EXPECT_TRUE(TestCopyAssign()); - - struct S {}; - EXPECT_TRUE(TestCopyAssign()); - EXPECT_TRUE(TestCopyAssign()); - EXPECT_TRUE(TestCopyAssign()); - - class C { - public: - explicit C(C* c) : c_(c) {} - ~C() { delete c_; } - - private: - C* c_; - }; - EXPECT_TRUE(TestCopyAssign()); - EXPECT_TRUE(TestCopyAssign()); - EXPECT_TRUE(TestCopyAssign()); - - // Reason for ifndef: add_lvalue_reference in libc++ breaks for these cases -#ifndef _LIBCPP_VERSION - EXPECT_TRUE(TestCopyAssign()); - EXPECT_TRUE(TestCopyAssign()); - EXPECT_TRUE(TestCopyAssign()); - EXPECT_TRUE(TestCopyAssign()); -#endif // _LIBCPP_VERSION -} - -template -bool TestMoveAssign() { - return absl::is_move_assignable::value == - std::is_move_assignable::value; -} - -TEST(TypeTraitsTest, IsMoveAssignable) { - EXPECT_TRUE(TestMoveAssign()); - EXPECT_TRUE(TestMoveAssign()); - EXPECT_TRUE(TestMoveAssign()); - - struct S {}; - EXPECT_TRUE(TestMoveAssign()); - EXPECT_TRUE(TestMoveAssign()); - EXPECT_TRUE(TestMoveAssign()); - - class C { - public: - explicit C(C* c) : c_(c) {} - ~C() { delete c_; } - void operator=(const C&) = delete; - void operator=(C&&) = delete; - - private: - C* c_; - }; - EXPECT_TRUE(TestMoveAssign()); - EXPECT_TRUE(TestMoveAssign()); - EXPECT_TRUE(TestMoveAssign()); - - // Reason for ifndef: add_lvalue_reference in libc++ breaks for these cases -#ifndef _LIBCPP_VERSION - EXPECT_TRUE(TestMoveAssign()); - EXPECT_TRUE(TestMoveAssign()); - EXPECT_TRUE(TestMoveAssign()); - EXPECT_TRUE(TestMoveAssign()); -#endif // _LIBCPP_VERSION -} - namespace adl_namespace { struct DeletedSwap { -- cgit v1.2.3