Add _a literal for named arguments

This commit is contained in:
Dean Moldovan 2015-09-27 04:09:37 +02:00
parent a63a24f2d7
commit bc6010cc21
2 changed files with 28 additions and 0 deletions

View File

@ -3030,6 +3030,16 @@ struct UdlFormat {
}
};
template <typename Char>
struct UdlArg {
const Char *str;
template <typename T>
NamedArg<Char> operator=(T &&value) const {
return {str, std::forward<T>(value)};
}
};
} // namespace internal
inline namespace literals {
@ -3039,6 +3049,11 @@ operator"" _format(const char *s, std::size_t) { return {s}; }
inline internal::UdlFormat<wchar_t>
operator"" _format(const wchar_t *s, std::size_t) { return {s}; }
inline internal::UdlArg<char>
operator"" _a(const char *s, std::size_t) { return {s}; }
inline internal::UdlArg<wchar_t>
operator"" _a(const wchar_t *s, std::size_t) { return {s}; }
} // inline namespace literals
} // namespace fmt
#endif // FMT_USE_USER_DEFINED_LITERALS

View File

@ -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