From 126ac9ffbee0f8a80baf92debe6fcb075a00a2ca Mon Sep 17 00:00:00 2001 From: SimonBraunschmidtIBA <135622649+SimonBraunschmidtIBA@users.noreply.github.com> Date: Thu, 23 Oct 2025 21:08:25 +0200 Subject: [PATCH] fix: add signature_of specialization for r-value references (#515) * test: add Variant move test * fix: provide signature_of<_T&&> Provide signature_of specialization for rvalue references resp. allow when forwarding references resolve to rvalue references signature_of<_T&&> is needed e.g. for std::map::try_emplace() when the emplaced sdbus:: type is passed as rvalue reference, otherwise the static_assert "Unsupported D-Bus type ..." would trigger See https://github.com/Kistler-Group/sdbus-cpp/issues/513#issuecomment-3429733769 for an elaborate explanation Signed-off-by: Simon Braunschmidt --------- Signed-off-by: Simon Braunschmidt Co-authored-by: Stanislav Angelovic --- include/sdbus-c++/TypeTraits.h | 4 ++++ tests/unittests/Types_test.cpp | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/sdbus-c++/TypeTraits.h b/include/sdbus-c++/TypeTraits.h index fafe950..e9143fa 100644 --- a/include/sdbus-c++/TypeTraits.h +++ b/include/sdbus-c++/TypeTraits.h @@ -151,6 +151,10 @@ namespace sdbus { struct signature_of<_T&> : signature_of<_T> {}; + template + struct signature_of<_T&&> : signature_of<_T> + {}; + template <> struct signature_of { diff --git a/tests/unittests/Types_test.cpp b/tests/unittests/Types_test.cpp index 647bc0b..995f091 100644 --- a/tests/unittests/Types_test.cpp +++ b/tests/unittests/Types_test.cpp @@ -97,6 +97,29 @@ TEST(AVariant, CanBeCopied) ASSERT_THAT(variantCopy2.get(), Eq(value)); } +TEST(AVariant, CanBeMoved) +{ + auto value = "hello"s; + sdbus::Variant variant(value); + + auto movedVariant{std::move(variant)}; + + ASSERT_THAT(movedVariant.get(), Eq(value)); + ASSERT_TRUE(variant.isEmpty()); +} + +TEST(AVariant, CanBeMovedIntoAMap) +{ + auto value = "hello"s; + sdbus::Variant variant(value); + + std::map mymap; + mymap.try_emplace("payload", std::move(variant)); + + ASSERT_THAT(mymap["payload"].get(), Eq(value)); + ASSERT_TRUE(variant.isEmpty()); +} + TEST(AVariant, IsNotEmptyWhenContainsAValue) { sdbus::Variant v("hello");