From 2dae92f81fea258cfbcb9065bd76d0f6d40bf391 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Wed, 7 May 2025 11:03:14 -0600 Subject: [PATCH] :bug: Make `field_name` assignment operation `const` Problem: - `field_name` is an empty type, and `field_name` assignment is an overloaded operator that returns a different type. Without making it `const`-qualified it cannot be called on template arguments. Solution: - `const`-qualify `field_name::operator=`. --- include/msg/message.hpp | 2 +- test/msg/message.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/msg/message.hpp b/include/msg/message.hpp index 9679856d..22fd9c69 100644 --- a/include/msg/message.hpp +++ b/include/msg/message.hpp @@ -128,7 +128,7 @@ template struct field_name { using name_t = stdx::cts_t; // NOLINTNEXTLINE(misc-unconventional-assign-operator) - template constexpr auto operator=(T value) { + template constexpr auto operator=(T value) const { return field_value{value}; } diff --git a/test/msg/message.cpp b/test/msg/message.cpp index f81044b7..15d0842d 100644 --- a/test/msg/message.cpp +++ b/test/msg/message.cpp @@ -72,6 +72,15 @@ TEST_CASE("construct with field values", "[message]") { CHECK(0x0042'd00d == data[1]); } +TEST_CASE("use field names as template args", "[message]") { + auto msg = []() { + return test_msg{F = 0xba11}; + }.template operator()<"f1"_field>(); + + auto const data = msg.data(); + CHECK(0x8000'ba11 == data[0]); +} + TEST_CASE("construct with field defaults", "[message]") { test_msg msg{};