From 6c9ef200105fc37b81cfd4a1b7871e61d2bc1c90 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 22 Jul 2021 19:01:05 +0430 Subject: [PATCH] AK: Add a CommonType type trait Also adds a simple-ish test for CommonType. --- AK/StdLibExtraDetails.h | 28 +++++++++++++++++++++++++--- Tests/AK/TestTypeTraits.cpp | 20 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/AK/StdLibExtraDetails.h b/AK/StdLibExtraDetails.h index 0f1cc687d8..748e353dc5 100644 --- a/AK/StdLibExtraDetails.h +++ b/AK/StdLibExtraDetails.h @@ -319,6 +319,30 @@ struct __MakeSigned { template using MakeSigned = typename __MakeSigned::Type; +template +auto declval() -> T; + +template +struct __CommonType; + +template +struct __CommonType { + using Type = T; +}; + +template +struct __CommonType { + using Type = decltype(true ? declval() : declval()); +}; + +template +struct __CommonType { + using Type = typename __CommonType::Type, Ts...>::Type; +}; + +template +using CommonType = typename __CommonType::Type; + template inline constexpr bool IsVoid = IsSame>; @@ -457,9 +481,6 @@ inline constexpr bool IsTrivial = __is_trivial(T); template inline constexpr bool IsTriviallyCopyable = __is_trivially_copyable(T); -template -auto declval() -> T; - template inline constexpr bool IsCallableWithArguments = requires(T t) { t(declval()...); }; @@ -515,6 +536,7 @@ inline constexpr bool IsTriviallyMoveAssignable = IsTriviallyAssignable; + EXPECT_VARIADIC_TRAIT_TRUE(IsSame, TCommon0, float); + + using TCommon1 = CommonType; + EXPECT_VARIADIC_TRAIT_TRUE(IsSame, TCommon1, int); + + struct Foo { + }; + using TCommon2 = CommonType; + EXPECT_VARIADIC_TRAIT_TRUE(IsSame, TCommon2, Foo); + + struct Bar { + operator Foo(); + }; + using TCommon3 = CommonType; + EXPECT_VARIADIC_TRAIT_TRUE(IsSame, TCommon3, Foo); +}