Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages #357

Closed
foonathan opened this issue Jul 15, 2016 · 8 comments
Closed

Better error messages #357

foonathan opened this issue Jul 15, 2016 · 8 comments

Comments

@foonathan
Copy link
Contributor

If you pass in a type that cannot be formatted, the error messages aren't.. exactly nice.

There are strategies to provide better error messages, namely a fallback functions that gets chosen as last resort with a static_assert() upon instantiation (I wrote a blog post if you are not aware of such techniques).

It would be nice if you implement something like this.

@vitaut
Copy link
Contributor

vitaut commented Jul 15, 2016

Thanks for the suggestions. I agree that it would be nice to improve error reporting in this case. The only problem is that the most general template-based overload is already taken for std::ostream support here:

template <typename Char, typename ArgFormatter, typename T>

Great blog post BTW!

@foonathan
Copy link
Contributor Author

Hm, maybe disable it via expression SFINAE (on compilers that support it...)?
And then add a fallback.

@xaxxon
Copy link

xaxxon commented Aug 14, 2016

Is this the "not exactly nice" error message?

/usr/local/include/fmt/format.h:1182:5: error: no matching function for call to 'format'
format(static_cast<Formatter>(formatter),
^~~~~~
/usr/local/include/fmt/format.h:1273:22: note: in instantiation of function template specialization
'fmt::internal::MakeValue<fmt::BasicFormatter<char, fmt::ArgFormatter > >::format_custom_arg' requested here
custom.format = &format_custom_arg;
^
/usr/local/include/fmt/format.h:2081:20: note: in instantiation of function template specialization
'fmt::internal::MakeValue<fmt::BasicFormatter<char, fmt::ArgFormatter > >::MakeValue' requested here
Value result = MakeValue(value);
^
/usr/local/include/fmt/format.h:3436:1: note: in instantiation of function template specialization 'fmt::internal::ArgArray<1,
true>::make<fmt::BasicFormatter<char, fmt::ArgFormatter >, MapCoordinate>' requested here
FMT_VARIADIC(std::string, format, CStringRef)
^
/usr/local/include/fmt/format.h:3408:3: note: expanded from macro 'FMT_VARIADIC'
FMT_VARIADIC_(char, ReturnType, func, return func, VA_ARGS)
^
/usr/local/include/fmt/format.h:3342:26: note: expanded from macro 'FMT_VARIADIC_'
ArgArray::template makefmt::BasicFormatter(args)...};
^
/Users/xaxxon/apb/include/utility.h:122:16: note: in instantiation of function template specialization 'fmt::format' requested here
reason(fmt::format(reason, std::forward(ts)...))
^
/Users/xaxxon/apb/src/map.cpp:52:9: note: in instantiation of function template specialization 'ApbException::ApbException<const MapCoordinate &>'
requested here
throw ApbException("Position not in map {}", position);
^
/usr/local/include/fmt/format.h:3436:27: note: candidate function [with Args = <const char *, MapCoordinate>] not viable: no known conversion from
'fmt::BasicFormatter<char, fmt::ArgFormatter >' to 'CStringRef' (aka 'BasicCStringRef') for 1st argument
FMT_VARIADIC(std::string, format, CStringRef)
^
/usr/local/include/fmt/format.h:3408:35: note: expanded from macro 'FMT_VARIADIC'
FMT_VARIADIC_(char, ReturnType, func, return func, VA_ARGS)
^
/usr/local/include/fmt/format.h:3338:14: note: expanded from macro 'FMT_VARIADIC_'
ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, VA_ARGS),
^
/usr/local/include/fmt/format.h:3437:30: note: candidate function [with Args = <const char *, MapCoordinate>] not viable: no known conversion from
'fmt::BasicFormatter<char, fmt::ArgFormatter >' to 'WCStringRef' (aka 'BasicCStringRef<wchar_t>') for 1st argument
FMT_VARIADIC_W(std::wstring, format, WCStringRef)
^
/usr/local/include/fmt/format.h:3411:38: note: expanded from macro 'FMT_VARIADIC_W'
FMT_VARIADIC_(wchar_t, ReturnType, func, return func, VA_ARGS)
^
/usr/local/include/fmt/format.h:3338:14: note: expanded from macro 'FMT_VARIADIC_'
ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, VA_ARGS),
^
/usr/local/include/fmt/format.h:3147:21: note: candidate function not viable: requires 2 arguments, but 3 were provided
inline std::wstring format(WCStringRef format_str, ArgList args) {
^
/usr/local/include/fmt/format.h:3141:20: note: candidate function not viable: requires 2 arguments, but 3 were provided
inline std::string format(CStringRef format_str, ArgList args) {
^

@vitaut
Copy link
Contributor

vitaut commented Aug 14, 2016

@xaxxon Yes, looks like it.

vitaut added a commit that referenced this issue Aug 14, 2016
@vitaut
Copy link
Contributor

vitaut commented Aug 14, 2016

Improved error reporting is implemented in 31e7e80. Now it will give:

...
fmt/format.h:1156:43: error: static assertion failed: Cannot format argument. To 
enable the use of ostream operator<< include fmt/ostream.h. Otherwise provide 
an overload of format_arg.

@xaxxon
Copy link

xaxxon commented Aug 14, 2016

That's awesome.

On Sun, Aug 14, 2016 at 7:31 AM, Victor Zverovich notifications@github.com
wrote:

Improved error reporting is implemented in 31e7e80
31e7e80.
Now it will give:

...
fmt/format.h:1156:43: error: static assertion failed: Cannot format argument. To
enable the use of ostream operator<< include fmt/ostream.h. Otherwise provide
an overload of format_arg.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#357 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAIyco2wl5mm5h-hlymePtSVIcIhcEjAks5qfybWgaJpZM4JNSQz
.

vitaut added a commit that referenced this issue Aug 16, 2016
@vitaut vitaut closed this as completed Aug 16, 2016
foonathan pushed a commit that referenced this issue Oct 23, 2016
(cherry picked from commit f19d8f9)
@courtney-g
Copy link

I'm trying to use fmt::hex and am seeing this error. I tried adding #include "fmt/ostream.h" and then get:

external/fmtlib/fmt/ostream.h:82:10: error: invalid operands to binary expression ('std::basic_ostream' and 'const fmt::IntFormatSpec<unsigned int, fmt::TypeSpec<'x'>, char>')
output << value;

...

Suggestions?

@vitaut
Copy link
Contributor

vitaut commented Mar 17, 2017

It looks that you are trying to use fmt::hex with iostreams while it should be used with a writer. If this is not the case please post the code that causes the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants