diff --git a/include/fmt/format.h b/include/fmt/format.h index 4e96539f..e86a8aa2 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3336,14 +3336,14 @@ arg_join, wchar_t> join(const Range& range, \endrst */ template inline std::string to_string(const T& value) { - return format("{}", value); + return fmt::format("{}", value); } /** Converts *value* to ``std::wstring`` using the default format for type *T*. */ template inline std::wstring to_wstring(const T& value) { - return format(L"{}", value); + return fmt::format(L"{}", value); } template diff --git a/test/format-test.cc b/test/format-test.cc index b0a8fc88..fbbf8a24 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2043,9 +2043,31 @@ TEST(FormatTest, DynamicFormatter) { "precision not allowed for this argument type"); } +namespace unrelated { + +struct qwe{}; + +std::string format(const char * /*fmt*/, const qwe &) { + // Return distinct string on purpose to check whether fmt used + // this format() via ADL by mistake + return "{unrelated free-standing format() function discovered by ADL by mistake}"; +} +} // namespace unrelated + +template<> +struct fmt::formatter { + template + auto parse(ParseContext & ctx) -> decltype(ctx.begin()) { return ctx.begin(); } + template + auto format(const unrelated::qwe &, FormatContext & ctx) -> decltype(ctx.out()) { + return fmt::format_to(ctx.out(), "[qwe]"); + } +}; + TEST(FormatTest, ToString) { EXPECT_EQ("42", fmt::to_string(42)); EXPECT_EQ("0x1234", fmt::to_string(reinterpret_cast(0x1234))); + EXPECT_EQ("[qwe]", fmt::to_string(unrelated::qwe{})); } TEST(FormatTest, ToWString) { EXPECT_EQ(L"42", fmt::to_wstring(42)); }