From a915a67897ac12cd455fc37d6509b5d42fec2155 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 28 Jul 2026 15:42:33 -0700 Subject: [PATCH] Handle const-qualified named arguments A named argument that arrives const-qualified was not recognized by is_named_arg, silently dropping its name. This happens both when a named argument is passed through an intermediate function returning const T& (https://github.com/fmtlib/fmt/issues/4866) and in fmt's own compiled format path, which passes arguments as const T&. Make is_named_arg and is_static_named_arg see through top-level const so the name is preserved instead of dropped. --- .github/workflows/linux.yml | 8 ++++++-- include/fmt/base.h | 4 ++++ test/compile-test.cc | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 70260dd9..5ba78e86 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -83,7 +83,9 @@ jobs: sudo apt update sudo apt install libatomic1 libc6-dev libgomp1 libitm1 libmpc3 # https://launchpad.net/ubuntu/xenial/amd64/g++-4.9/4.9.3-13ubuntu2 - wget --no-verbose \ + # launchpad periodically returns 503s, so retry to avoid flaky CI. + wget --no-verbose --tries=5 --waitretry=10 \ + --retry-connrefused --retry-on-http-error=503 \ http://launchpadlibrarian.net/230069137/libmpfr4_3.1.3-2_amd64.deb \ http://launchpadlibrarian.net/253728424/libasan1_4.9.3-13ubuntu2_amd64.deb \ http://launchpadlibrarian.net/445346135/libubsan0_5.4.0-6ubuntu1~16.04.12_amd64.deb \ @@ -114,7 +116,9 @@ jobs: sudo apt update sudo apt install libtinfo5 # https://code.launchpad.net/ubuntu/xenial/amd64/clang-3.6/1:3.6.2-3ubuntu2 - wget --no-verbose \ + # launchpad periodically returns 503s, so retry to avoid flaky CI. + wget --no-verbose --tries=5 --waitretry=10 \ + --retry-connrefused --retry-on-http-error=503 \ http://launchpadlibrarian.net/230019046/libffi6_3.2.1-4_amd64.deb \ http://launchpadlibrarian.net/445346109/libasan2_5.4.0-6ubuntu1~16.04.12_amd64.deb \ http://launchpadlibrarian.net/445346135/libubsan0_5.4.0-6ubuntu1~16.04.12_amd64.deb \ diff --git a/include/fmt/base.h b/include/fmt/base.h index f213fa09..ea9ebf61 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -1059,6 +1059,10 @@ template struct is_static_named_arg : std::false_type {}; template struct is_named_arg> : std::true_type {}; +template struct is_named_arg : is_named_arg {}; +template +struct is_static_named_arg : is_static_named_arg {}; + template struct named_arg : view { const Char* name; const T& value; diff --git a/test/compile-test.cc b/test/compile-test.cc index 476bcb43..c3331273 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -163,6 +163,10 @@ TEST(compile_test, named) { fmt::format(FMT_COMPILE("{a0} {a1}"), "a0"_a = 41, "a1"_a = 43)); EXPECT_EQ("41 43", fmt::format(FMT_COMPILE("{a1} {a0}"), "a0"_a = 43, "a1"_a = 41)); + + // A statically-named argument with a format spec compiles to spec_field, + // which passes the argument to make_format_args as const (#4866). + EXPECT_EQ("4.2", fmt::format(FMT_COMPILE("{arg:3.1f}"), "arg"_a = 4.2)); # endif }