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

<format>: Call to next_arg_id may result in unexpected error (regression) #4636

Closed
JMazurkiewicz opened this issue Apr 27, 2024 · 0 comments · Fixed by #4640
Closed

<format>: Call to next_arg_id may result in unexpected error (regression) #4636

JMazurkiewicz opened this issue Apr 27, 2024 · 0 comments · Fixed by #4640
Labels
bug Something isn't working fixed Something works now, yay! format C++20/23 format

Comments

@JMazurkiewicz
Copy link
Contributor

JMazurkiewicz commented Apr 27, 2024

When user-defined formatter calls next_arg_id and format-spec is empty we will get compiler error.

Example:

#include <print>

struct FormatNextArg {};

template <> struct std::formatter<FormatNextArg> {
public:
  template <class ParseContext> constexpr auto parse(ParseContext &ctx) {
    auto it = ctx.begin();
    if (it != ctx.end() && *it != '}') {
      throw std::format_error{"Expected empty spec"};
    }

    arg_id = ctx.next_arg_id();
    return it;
  }

  template <class FormatContext>
  auto format(FormatNextArg, FormatContext &ctx) const {
    return std::format_to(ctx.out(), "arg-id: {}", arg_id);
  }

private:
  size_t arg_id;
};

int main() {
  // std::println("{}, {}", FormatNextArg{}, 0, FormatNextArg{}, "1"); // Does not compile since VS 17.10
  std::println("{:}, {:}", FormatNextArg{}, 2, FormatNextArg{}, "3"); // Works, replacement field is not empty
}

Introduced by: #4078
Compiler explorer (VS 17.9): https://godbolt.org/z/rsje4j1hP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay! format C++20/23 format
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants