From 9c9185d72c56898b7eaba9bbccb88740dbf40f71 Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Tue, 15 Jun 2021 18:53:34 +0300 Subject: [PATCH 1/5] type traits solution --- .../type_traits/is_copy_constructible.h | 63 +++++++++++++--- .../is_nothrow_move_constructible.h | 30 ++++++-- .../TypeTraits/type_traits/move_if_noexcept.h | 22 +++--- .../homework/TypeTraits/type_traits/utility.h | 75 +++++++++++++++++-- 4 files changed, 156 insertions(+), 34 deletions(-) diff --git a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h index b02d9f02..6922a70f 100644 --- a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h +++ b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h @@ -10,29 +10,74 @@ struct LibCppIsConstructible; template struct IsInvalidBaseToDerivedCast { - // Your code goes here + using base = uncvref_t; + using derived = uncvref_t; + + static std::integral_constant && + std::is_base_of_v && + !LibCppIsConstructible::type::value> + value_; }; template struct IsInvalidLvalueToRvalueCast : std::false_type { - // Your code goes here }; template struct IsInvalidLvalueToRvalueCast { - // Your code goes here + using from = uncvref_t; + using to = uncvref_t; + using value = + std::integral_constant && + std::is_base_of_v>; }; struct IsConstructibleHelper { - // Your code goes here + template + static void ImplicitCast(To); + + template (Declval()))> + static std::true_type Castable(int); + + template (Declval()))> + static std::integral_constant::value && + !IsInvalidLvalueToRvalueCast::value> + Castable(double); + + template + static std::false_type Castable(...); + + template ()...))> + static std::true_type Constructible(int); + + template + static std::false_type Constructible(...); +}; + +template +struct LibCppIsConstructible { + using type = decltype(IsConstructibleHelper::Constructible(0)); +}; + +template +struct LibCppIsConstructible { + using type = decltype(IsConstructibleHelper::Constructible(0)); }; -// LibCppIsConstructible - partial specializations -// Your code goes here -// LibCppIsConstructible - partial specializations +template +struct LibCppIsConstructible { + using type = decltype(IsConstructibleHelper::Castable(0)); +}; + +template +struct LibCppIsConstructible { + using type = decltype(IsConstructibleHelper::Castable(0)); +}; template -struct IsConstructible : // Your code goes here {...} +struct IsConstructible : LibCppIsConstructible::type {}; template -struct IsCopyConstructible : // Your code goes here {...} \ No newline at end of file +struct IsCopyConstructible : LibCppIsConstructible::type {}; \ No newline at end of file diff --git a/module-1/homework/TypeTraits/type_traits/is_nothrow_move_constructible.h b/module-1/homework/TypeTraits/type_traits/is_nothrow_move_constructible.h index bd749521..0e744752 100644 --- a/module-1/homework/TypeTraits/type_traits/is_nothrow_move_constructible.h +++ b/module-1/homework/TypeTraits/type_traits/is_nothrow_move_constructible.h @@ -9,15 +9,31 @@ template struct LibCppIsNoThrowConstructible; -// LibCppIsNoThrowConstructible - partial specializations -// Your code goes here -// LibCppIsNoThrowConstructible - partial specializations +template +struct LibCppIsNoThrowConstructible { + using type = std::integral_constant()...))>; +}; + +template +struct LibCppIsNoThrowConstructible { + using type = + std::integral_constant(Declval()))>; +}; + +template +struct LibCppIsNoThrowConstructible { + using type = std::false_type; +}; template -struct IsNoThrowConstructible : // Your code goes here {...} +struct IsNoThrowConstructible + : LibCppIsNoThrowConstructible::value, std::is_reference_v, T, + Args...>::type {}; -template -struct IsNoThrowConstructible : // Your code goes here {...} +template +struct IsNoThrowConstructible + : LibCppIsNoThrowConstructible::value, std::is_reference_v, T>::type {}; template -struct IsNoThrowMoveConstructible : // Your code goes here {...} \ No newline at end of file +struct IsNoThrowMoveConstructible : IsNoThrowConstructible {}; \ No newline at end of file diff --git a/module-1/homework/TypeTraits/type_traits/move_if_noexcept.h b/module-1/homework/TypeTraits/type_traits/move_if_noexcept.h index 4ce67cec..37ba8e82 100644 --- a/module-1/homework/TypeTraits/type_traits/move_if_noexcept.h +++ b/module-1/homework/TypeTraits/type_traits/move_if_noexcept.h @@ -8,13 +8,12 @@ #include "utility.h" template -struct Conditional { - // Your code goes here -}; +struct Conditional; -// Conditional - partial specialization -// Your code goes here -// Conditional - partial specialization +template +struct Conditional { + using type = F; +}; template struct Conditional { @@ -22,8 +21,11 @@ struct Conditional { }; template -using conditional_v = // Your code goes here +using conditional_v = typename Conditional::type; -// MoveIfNoExcept -// Your code goes here -// MoveIfNoExcept \ No newline at end of file +template +typename Conditional::value && IsCopyConstructible::value, + const T&, T&&>::type +MoveIfNoExcept(T& x) noexcept { + return std::move(x); +} \ No newline at end of file diff --git a/module-1/homework/TypeTraits/type_traits/utility.h b/module-1/homework/TypeTraits/type_traits/utility.h index 1f0dc419..41d9dc8c 100644 --- a/module-1/homework/TypeTraits/type_traits/utility.h +++ b/module-1/homework/TypeTraits/type_traits/utility.h @@ -3,30 +3,89 @@ #include #include +template +struct RemoveConst { + using type = T; +}; + +template +struct RemoveConst { + using type = T; +}; + +template +using remove_const_t = typename RemoveConst::type; + +template +struct RemoveVolatile { + using type = T; +}; + +template +struct RemoveVolatile { + using type = T; +}; + +template +using remove_volatile_t = typename RemoveVolatile::type; + +template +struct RemoveReference { + using type = T; +}; + +template +struct RemoveReference { + using type = T; +}; + +template +struct RemoveReference { + using type = T; +}; + +template +using remove_reference_t = typename RemoveReference::type; + +template +struct Uncv { + using type = remove_const_t>; +}; + +template +using uncv_t = typename Uncv::type; + template struct Uncvref { - // Your code goes here + using type = uncv_t>; }; template -using uncvref_t = // Your code goes here +using uncvref_t = typename Uncvref::type; template struct AddConst { - using type = // Your code goes here + using type = const T; }; template -using add_const_t = // Your code goes here +using add_const_t = typename AddConst::type; template -struct AddLvalueReference : // Your code goes here +struct AddLvalueReference { + using type = T&; +}; + +template +struct AddRvalueReference { + using type = T&&; +}; template -struct AddRvalueReference : // Your code goes here +using add_lvalue_reference_t = typename AddLvalueReference::type; template -using add_lvalue_reference_t = // Your code goes here +using add_rvalue_reference_t = typename AddRvalueReference::type; template -using add_rvalue_reference_t = // Your code goes here \ No newline at end of file +add_rvalue_reference_t Declval() noexcept; \ No newline at end of file From 05f3dc2d349b28ae1df62ea8dfd70de7ee42a7ea Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Tue, 15 Jun 2021 18:57:50 +0300 Subject: [PATCH 2/5] fix style 1.0 --- .../TypeTraits/type_traits/is_copy_constructible.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h index 6922a70f..ccfcbb8c 100644 --- a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h +++ b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h @@ -14,10 +14,10 @@ struct IsInvalidBaseToDerivedCast { using derived = uncvref_t; static std::integral_constant && - std::is_base_of_v && - !LibCppIsConstructible::type::value> - value_; + !std::is_same_v && + std::is_base_of_v && + !LibCppIsConstructible::type::value> + value_; }; template @@ -29,8 +29,8 @@ struct IsInvalidLvalueToRvalueCast { using from = uncvref_t; using to = uncvref_t; using value = - std::integral_constant && + std::integral_constant && std::is_base_of_v>; }; From 4e7b65e55b01cf9585715cd1412bfe8238ca691c Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Tue, 15 Jun 2021 19:02:46 +0300 Subject: [PATCH 3/5] fix style 1.1 --- .../type_traits/is_copy_constructible.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h index ccfcbb8c..4cee22ae 100644 --- a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h +++ b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h @@ -14,24 +14,26 @@ struct IsInvalidBaseToDerivedCast { using derived = uncvref_t; static std::integral_constant && - std::is_base_of_v && - !LibCppIsConstructible::type::value> - value_; + !std::is_same_v && + std::is_base_of_v && + !LibCppIsConstructible::type::value> + value_; }; template struct IsInvalidLvalueToRvalueCast : std::false_type { + // Your code goes here }; template struct IsInvalidLvalueToRvalueCast { using from = uncvref_t; using to = uncvref_t; - using value = - std::integral_constant && - std::is_base_of_v>; + + static std::integral_constant && + std::is_base_of_v> + value_; }; struct IsConstructibleHelper { From 906db8f87df08e0292ffe6b2eb34878340bf63e7 Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Tue, 15 Jun 2021 19:09:03 +0300 Subject: [PATCH 4/5] fix style 1.2 --- .../type_traits/is_copy_constructible.h | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h index 4cee22ae..21ae1992 100644 --- a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h +++ b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h @@ -5,35 +5,29 @@ #include "utility.h" + template struct LibCppIsConstructible; template struct IsInvalidBaseToDerivedCast { - using base = uncvref_t; using derived = uncvref_t; - - static std::integral_constant && - std::is_base_of_v && - !LibCppIsConstructible::type::value> - value_; + using base = uncvref_t; + using value = + std::integral_constant && + std::is_base_of_v && + !LibCppIsConstructible::type::value>; }; template -struct IsInvalidLvalueToRvalueCast : std::false_type { - // Your code goes here -}; +struct IsInvalidLvalueToRvalueCast : std::false_type {}; template struct IsInvalidLvalueToRvalueCast { - using from = uncvref_t; using to = uncvref_t; - - static std::integral_constant && - std::is_base_of_v> - value_; + using from = uncvref_t; + using value = + std::integral_constant && std::is_base_of_v>; }; struct IsConstructibleHelper { From 42fb86d3326c6df92806ba1e3a4fd263a439bfff Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Tue, 15 Jun 2021 19:10:41 +0300 Subject: [PATCH 5/5] fix style 1.3 --- module-1/homework/TypeTraits/type_traits/is_copy_constructible.h | 1 - 1 file changed, 1 deletion(-) diff --git a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h index 21ae1992..8634d2c9 100644 --- a/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h +++ b/module-1/homework/TypeTraits/type_traits/is_copy_constructible.h @@ -5,7 +5,6 @@ #include "utility.h" - template struct LibCppIsConstructible;