From aa9b09a9e3bece9735560ff26fd1f9ed7a663e3f Mon Sep 17 00:00:00 2001 From: Jonathan Gopel Date: Thu, 12 Nov 2020 11:10:52 -0700 Subject: [PATCH] :bug: Cannot call non-constexpr function in constexpr context (#2010) Problem: - gcc-8 gives the following error when compiling this function on all standards: test/std-format-test.cc: In member function 'constexpr auto std::formatter::parse(std::format_parse_context&)': test/std-format-test.cc:112:17: error: call to non-'constexpr' function 'int isdigit(int)' if (!isdigit(c) || (++iter, get_char()) != '}') ~~~~~~~^~~ Solution: - Write a `constexpr` version of `isdigit` for use in this function. Co-authored-by: Jonathan Gopel --- test/std-format-test.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/std-format-test.cc b/test/std-format-test.cc index b305ad85..916a7c5e 100644 --- a/test/std-format-test.cc +++ b/test/std-format-test.cc @@ -103,13 +103,17 @@ template <> struct std::formatter { // Parses a width argument id in the format { }. constexpr auto parse(format_parse_context& ctx) { + constexpr auto is_ascii_digit = [](const char c) { + return c >= '0' && c <= '9'; + }; + auto iter = ctx.begin(); // auto get_char = [&]() { return iter != ctx.end() ? *iter : 0; }; auto get_char = [&]() { return iter != ctx.end() ? *iter : '\0'; }; if (get_char() != '{') return iter; ++iter; char c = get_char(); - if (!isdigit(c) || (++iter, get_char()) != '}') + if (!is_ascii_digit(c) || (++iter, get_char()) != '}') throw format_error("invalid format"); width_arg_id = fmt::detail::to_unsigned(c - '0'); ctx.check_arg_id(width_arg_id);