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 Error "error C2668" when try formatting user-defined type. #2574

Closed
XiangYyang opened this issue Oct 30, 2021 · 1 comment
Closed

Comments

@XiangYyang
Copy link

A simple code from https://fmt.dev/latest/api.html?highlight=formatter:

#include ...

struct point { double x, y; };

template <> struct fmt::formatter<point> {
  // Presentation format: 'f' - fixed, 'e' - exponential.
  char presentation = 'f';

  // Parses format specifications of the form ['f' | 'e'].
  constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
    // [ctx.begin(), ctx.end()) is a character range that contains a part of
    // the format string starting from the format specifications to be parsed,
    // e.g. in
    //
    //   fmt::format("{:f} - point of interest", point{1, 2});
    //
    // the range will contain "f} - point of interest". The formatter should
    // parse specifiers until '}' or the end of the range. In this example
    // the formatter should parse the 'f' specifier and return an iterator
    // pointing to '}'.

    // Parse the presentation format and store it in the formatter:
    auto it = ctx.begin(), end = ctx.end();
    if (it != end && (*it == 'f' || *it == 'e')) presentation = *it++;

    // Check if reached the end of the range:
    if (it != end && *it != '}')
      throw format_error("invalid format");

    // Return an iterator past the end of the parsed range:
    return it;
  }

  // Formats the point p using the parsed format specification (presentation)
  // stored in this formatter.
  template <typename FormatContext>
  auto format(const point& p, FormatContext& ctx) -> decltype(ctx.out()) {
    // ctx.out() is an output iterator to write to.
    return format_to(
        ctx.out(),
        presentation == 'f' ? "({:.1f}, {:.1f})" : "({:.1e}, {:.1e})",
        p.x, p.y);
  }
};

then

point fuck = { 1, 2 };
fmt::print("{}\n", fuck);

Using MSVC++ Compiler 16.11.0 to build with C++20 (I use consteval in my code, so, I must use C++20), cause an compile error:

error C2668: “fmt::v8::format_to”: ambiguous call to overloaded function

It said that there are two functions can be called:

OutputIt fmt::v8::format_to<OutputIt,const double&,const double&,0>(OutputIt,fmt::v8::basic_format_string<char,const double &,const double &>,const double &,const double &)
          with
          [
              OutputIt=fmt::v8::appender
          ]

OR:

_OutputIt std::format_to<OutputIt,double,double>(_OutputIt,const std::string_view,const double &,const double &)
          with
          [
              _OutputIt=fmt::v8::appender,
              OutputIt=fmt::v8::appender
          ]

I don't know what happend, I need a help.

Thanks.

@vitaut
Copy link
Contributor

vitaut commented Oct 30, 2021

Looks like the same issue as #2522. Please use an updated example from https://fmt.dev/dev/api.html#formatting-user-defined-types.

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