Improve error reporting

This commit is contained in:
Victor Zverovich 2020-07-14 11:13:21 -07:00
parent f4b11ef6e2
commit c26349f4d2
2 changed files with 10 additions and 11 deletions

View File

@ -1214,6 +1214,8 @@ struct is_contiguous<std::basic_string<Char>> : std::true_type {};
template <typename Char>
struct is_contiguous<detail::buffer<Char>> : std::true_type {};
template <typename T> struct formattable : std::false_type {};
namespace detail {
template <typename OutputIt>
@ -1258,14 +1260,11 @@ FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T& value) {
return arg;
}
template <typename T> struct formattable : std::false_type {};
template <typename T> int check(unformattable) {
static_assert(
formattable<T>(),
"Cannot format argument. To make type T formattable provide a "
"formatter<T> specialization: "
"https://fmt.dev/latest/api.html#formatting-user-defined-types");
"Cannot format an argument. To make type T formattable provide a "
"formatter<T> specialization: https://fmt.dev/dev/api.html#udt");
return 0;
}
template <typename T, typename U> inline const U& check(const U& val) {

View File

@ -114,20 +114,20 @@ TEST(CompileTest, MultipleTypes) {
EXPECT_EQ(fmt::format(f, 42, 42), "42 42");
}
struct formattable {};
struct test_formattable {};
FMT_BEGIN_NAMESPACE
template <> struct formatter<formattable> : formatter<const char*> {
template <> struct formatter<test_formattable> : formatter<const char*> {
template <typename FormatContext>
auto format(formattable, FormatContext& ctx) -> decltype(ctx.out()) {
auto format(test_formattable, FormatContext& ctx) -> decltype(ctx.out()) {
return formatter<const char*>::format("foo", ctx);
}
};
FMT_END_NAMESPACE
TEST(CompileTest, FormatUserDefinedType) {
auto f = fmt::detail::compile<formattable>("{}");
EXPECT_EQ(fmt::format(f, formattable()), "foo");
auto f = fmt::detail::compile<test_formattable>("{}");
EXPECT_EQ(fmt::format(f, test_formattable()), "foo");
}
TEST(CompileTest, EmptyFormatString) {
@ -146,7 +146,7 @@ TEST(CompileTest, FormatDefault) {
EXPECT_EQ("4.2", fmt::format(FMT_COMPILE("{}"), 4.2));
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), "foo"));
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo")));
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), formattable()));
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), test_formattable()));
}
TEST(CompileTest, FormatSpecs) {