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

Compile time format string fails for user defined types in 6.0.0 #1292

Closed
marack opened this issue Aug 30, 2019 · 2 comments
Closed

Compile time format string fails for user defined types in 6.0.0 #1292

marack opened this issue Aug 30, 2019 · 2 comments

Comments

@marack
Copy link

marack commented Aug 30, 2019

I'm finding that the compile time format strings via the _format UDL are failing for my user defined types in situations where they have previously worked. Here's a minimal example:

#include "fmt/format.h"

struct foo { };·

template <>
struct fmt::formatter<foo> : fmt::formatter<int>
{
  template <typename FmtCtx>
  auto format(foo const& f, FmCtx& ctx) { return ctx.out(); }
};

using namespace fmt::literals;

int main(int argc, char const* argv[])
{
  fmt::format("{}", foo{}); // okay
  "{}"_format(foo{}); // okay

  foo f;
  fmt::format("{}", f); // okay
  "{}"_format(f); // compilation error

  return 0;
}

Compilation error on the final UDL format which was passed a l-value foo indicates that fmt has not detected the custom formatter and is trying to use the fallback formatter:

.../fmt-6.0.0/include/fmt/format.h:2503:7: error: use of deleted function ‘fmt::v6::internal::fallback_formatter<T, Char, Enable>::fallback_formatter() [with T = foo&; Char = char; Enable = void]’

Most likely it is failing the has_formatter<T> trait. Since the custom formatter is found when passing a rvalue ("{}"_format(foo{})) perhaps there is a std::decay missing somewhere?

I'm using gcc 8.3.1, and fmt 6.0.0.

@marack
Copy link
Author

marack commented Aug 30, 2019

I've found that changing the call to do_check_format_string<> in the udl_formatter template so that it decays the Args parameter pack seems to resolve the problem:

From:

do_check_format_string<Char, error_handler, Args...>(

To:

do_check_format_string<Char, error_handler, typename std::decay<Args>::type...>(

This change still passes the built in unit tests.

@vitaut
Copy link
Contributor

vitaut commented Aug 31, 2019

Good catch, thanks! Fixed in 422e7b9.

@vitaut vitaut closed this as completed Aug 31, 2019
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

2 participants