From c6705faf2f1a4743e3d49038b37815f9b943da20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanislav=20Angelovi=C4=8D?= Date: Wed, 15 May 2024 15:46:24 +0200 Subject: [PATCH] fix: const enum signature_of ambiguity error (#444) This fixes the template specialization ambiguity error when a signature_of applied upon a const (and/or volatile) enum type would render the const specialization and the enum specialization as two equally-ranked candidates. --- include/sdbus-c++/TypeTraits.h | 10 +++++++--- tests/unittests/Message_test.cpp | 11 +++++++++-- tests/unittests/TypeTraits_test.cpp | 6 ++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/sdbus-c++/TypeTraits.h b/include/sdbus-c++/TypeTraits.h index 164cd15..5505ed2 100644 --- a/include/sdbus-c++/TypeTraits.h +++ b/include/sdbus-c++/TypeTraits.h @@ -140,6 +140,10 @@ namespace sdbus { struct signature_of : signature_of<_T> {}; + template + struct signature_of : signature_of<_T> + {}; + template struct signature_of<_T&> : signature_of<_T> {}; @@ -337,9 +341,9 @@ namespace sdbus { }; #endif - template - struct signature_of<_Enum, typename std::enable_if_t>> - : public signature_of> + template // is_const_v and is_volatile_v to avoid ambiguity conflicts with const and volatile specializations of signature_of + struct signature_of<_Enum, typename std::enable_if_t && !std::is_const_v<_Enum> && !std::is_volatile_v<_Enum>>> + : signature_of> {}; template diff --git a/tests/unittests/Message_test.cpp b/tests/unittests/Message_test.cpp index dd22903..eb158e4 100644 --- a/tests/unittests/Message_test.cpp +++ b/tests/unittests/Message_test.cpp @@ -93,17 +93,24 @@ struct sdbus::signature_of> {}; namespace my { + enum class Enum + { + Value1, + Value2 + }; + struct Struct { int i; std::string s; std::list l; + Enum e; friend bool operator==(const Struct& lhs, const Struct& rhs) = default; }; } -SDBUSCPP_REGISTER_STRUCT(my::Struct, i, s, l); +SDBUSCPP_REGISTER_STRUCT(my::Struct, i, s, l, e); /*-------------------------------------*/ /* -- TEST CASES -- */ @@ -487,7 +494,7 @@ TEST(AMessage, CanCarryDBusStructGivenAsCustomType) { auto msg = sdbus::createPlainMessage(); - const my::Struct dataWritten{3545342, "hello"s, {3.14, 2.4568546}}; + const my::Struct dataWritten{3545342, "hello"s, {3.14, 2.4568546}, my::Enum::Value2}; msg << dataWritten; msg.seal(); diff --git a/tests/unittests/TypeTraits_test.cpp b/tests/unittests/TypeTraits_test.cpp index be7464e..d0b8cdd 100644 --- a/tests/unittests/TypeTraits_test.cpp +++ b/tests/unittests/TypeTraits_test.cpp @@ -102,6 +102,9 @@ namespace TYPE(std::span)HAS_DBUS_TYPE_SIGNATURE("an") #endif TYPE(SomeEnumClass)HAS_DBUS_TYPE_SIGNATURE("y") + TYPE(const SomeEnumClass)HAS_DBUS_TYPE_SIGNATURE("y") + TYPE(volatile SomeEnumClass)HAS_DBUS_TYPE_SIGNATURE("y") + TYPE(const volatile SomeEnumClass)HAS_DBUS_TYPE_SIGNATURE("y") TYPE(SomeEnumStruct)HAS_DBUS_TYPE_SIGNATURE("x") TYPE(SomeClassicEnum)HAS_DBUS_TYPE_SIGNATURE("u") TYPE(std::map)HAS_DBUS_TYPE_SIGNATURE("a{ix}") @@ -157,6 +160,9 @@ namespace , std::span #endif , SomeEnumClass + , const SomeEnumClass + , volatile SomeEnumClass + , const volatile SomeEnumClass , SomeEnumStruct , SomeClassicEnum , std::map