From f75daa7c1a15330dde4225710d5d7810e8ce77ff Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Thu, 11 Jul 2024 09:44:28 +0300 Subject: [PATCH] add same_as concept --- README.md | 19 +++++++++++++++++++ include/form/form.h | 5 ++++- src/main.cpp | 1 + src/test.cpp | 1 - src/test.h | 20 ++++++++++++++++---- 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5111e31..f839634 100644 --- a/README.md +++ b/README.md @@ -190,3 +190,22 @@ void runTests() { form::run_tests<^for_tests>(); } */ ``` + +## extension of same_as concept to templates + +concept `form::same_as` checks if type represent specific template + +``` c++ + +template int foo(T) { return -1; } + +template + requires form::same_as +int foo(T) { + return 1; +} + + +foo(std::complex{1,1}); //1 + +``` diff --git a/include/form/form.h b/include/form/form.h index f6561c7..9a9f2bb 100644 --- a/include/form/form.h +++ b/include/form/form.h @@ -107,7 +107,7 @@ template consteval std::size_t get_padding() { std::size_t padding{0}; std::size_t pointer{offset_of(nonstatic_data_members_of(^S)[0])}; - [: util::expand(nonstatic_data_members_of(^S)):] >> [&, i = 0]() { + [:util::expand(nonstatic_data_members_of(^S)):] >> [&, i = 0]() { if (pointer == offset_of(e)) pointer += size_of(e); else { @@ -123,6 +123,9 @@ concept no_padding = std::same_as()>, std::integral_constant>; +template +concept same_as = template_of(^T) == refl; + } // namespace form template <> struct std::formatter> { diff --git a/src/main.cpp b/src/main.cpp index 23e4c01..a5753af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 #include +#include #include #include
#include diff --git a/src/test.cpp b/src/test.cpp index e3c75dc..1c932d0 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -2,6 +2,5 @@ int main() { form::run_tests<^form::tests>(); - form::run_tests<^form::round_trip_tests>(); form::run_tests<^form::examples>(); } diff --git a/src/test.h b/src/test.h index 1cf1552..bd55ae2 100644 --- a/src/test.h +++ b/src/test.h @@ -2,6 +2,7 @@ #pragma once #include #include +#include #include #include #include @@ -173,6 +174,18 @@ template constexpr auto CreateClass() { // clang-format on } // namespace detail + +namespace form_same_as { +template int foo(T) { return -1; } + +template + requires form::same_as +int foo(T) { + return 1; +} + +} // namespace form_same_as + namespace form::tests { void testZ() { @@ -230,9 +243,8 @@ void testSJson() { s.j = 2; std::println("{}", form::format_json(s)); } -} // namespace form::tests -namespace form::round_trip_tests { +bool CheckSameAs() { return form_same_as::foo(std::complex{1, 1}) == 1; } bool compareTrue() { return form::compare(3.0, 3.0) && form::compare(3, 3) && @@ -312,7 +324,7 @@ bool ConfigRoundTrip() { return round_trip(config); } -} // namespace form::round_trip_tests +} // namespace form::tests namespace list { struct CancelSelection {}; @@ -349,8 +361,8 @@ template void foo(T t) { std::println("Without padding"); } template void foo(T t) { std::println("With padding: {}", form::get_padding()); } - namespace form::examples { + bool VariantCreate() { list_variant v{list::CancelSelection{}}; return std::holds_alternative(v);