From bc6010cc211e20942948ae9bce4939dc1eea590f Mon Sep 17 00:00:00 2001 From: Dean Moldovan Date: Sun, 27 Sep 2015 04:09:37 +0200 Subject: [PATCH] Add _a literal for named arguments --- format.h | 15 +++++++++++++++ test/format-test.cc | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/format.h b/format.h index b85ddcb1..e5e91537 100644 --- a/format.h +++ b/format.h @@ -3030,6 +3030,16 @@ struct UdlFormat { } }; +template +struct UdlArg { + const Char *str; + + template + NamedArg operator=(T &&value) const { + return {str, std::forward(value)}; + } +}; + } // namespace internal inline namespace literals { @@ -3039,6 +3049,11 @@ operator"" _format(const char *s, std::size_t) { return {s}; } inline internal::UdlFormat operator"" _format(const wchar_t *s, std::size_t) { return {s}; } +inline internal::UdlArg +operator"" _a(const char *s, std::size_t) { return {s}; } +inline internal::UdlArg +operator"" _a(const wchar_t *s, std::size_t) { return {s}; } + } // inline namespace literals } // namespace fmt #endif // FMT_USE_USER_DEFINED_LITERALS diff --git a/test/format-test.cc b/test/format-test.cc index b95c499a..c189f990 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1610,4 +1610,17 @@ TEST(LiteralsTest, Format) { EXPECT_EQ(format("{}c{}", "ab", 1), "{}c{}"_format("ab", 1)); EXPECT_EQ(format(L"{}c{}", L"ab", 1), L"{}c{}"_format(L"ab", 1)); } + +TEST(LiteralsTest, NamedArg) { + EXPECT_EQ(format("{first}{second}{first}{third}", + fmt::arg("first", "abra"), fmt::arg("second", "cad"), + fmt::arg("third", 99)), + format("{first}{second}{first}{third}", + "first"_a="abra", "second"_a="cad", "third"_a=99)); + EXPECT_EQ(format(L"{first}{second}{first}{third}", + fmt::arg(L"first", L"abra"), fmt::arg(L"second", L"cad"), + fmt::arg(L"third", 99)), + format(L"{first}{second}{first}{third}", + L"first"_a=L"abra", L"second"_a=L"cad", L"third"_a=99)); +} #endif // FMT_USE_USER_DEFINED_LITERALS