Skip to content

regression in 'constexpr' expansion of 'fmt::v8::basic_format_string...'Β #2898

Closed

Description

@RalphSteinhagen

Hi, first of all: thanks for the great library! πŸ‘

While bumping versions, I may have found a regression starting from v8.0.1 (v7.1.3 tested OK).
The following code snippet (see also compiler-explorer)

#include <iostream>
#include <string>
#include <fmt/format.h>

template<bool strict = true>
std::string err_format(const char *fmt,  const auto &...errorArgs) {
    if constexpr (strict) {
        return fmt::format(fmt, errorArgs...);
    }
    return "do something else";
}

int main() {
    std::cout << err_format("exception: {} {}!", "Hello", "World");
}

yields the following compile-time error:

<source>: In instantiation of 'std::string err_format(const char*, const auto:14& ...) [with bool strict = true; auto:14 = {char [6], char [6]}; std::string = std::__cxx11::basic_string<char>]':
<source>:14:28:   required from here
<source>:8:27:   in 'constexpr' expansion of 'fmt::v8::basic_format_string<char, const char (&)[6], const char (&)[6]>(fmt)'
<source>:8:27: error: 'fmt' is not a constant expression
    8 |         return fmt::format(fmt, errorArgs...);

Tested this also with older gcc and clang versions with similar results.

Any ideas, hints or work-around suggestions would be much welcome! Thanks in advance!

Activity

  1. vitaut commented on May 17, 2022

    @vitaut
    Contributor

    {fmt} 8.x requires format strings to be known at compile time by default. Quoting release notes:

    • Enabled compile-time format string check by default. For example (godbolt):

      #include <fmt/core.h>
      
      int main() {
        fmt::print("{:d}", "I am not a number");
      }

      gives a compile-time error on compilers with C++20 consteval support (gcc 10+, clang 11+) because d is not a valid format specifier for a string.

      To pass a runtime string wrap it in fmt::runtime:

      fmt::print(fmt::runtime("{:d}"), "I am not a number");
  2. RalphSteinhagen commented on May 19, 2022

    @RalphSteinhagen
    Author

    @vitaut I understand the compile-time format evaluation/validateion.

    I believe the above issue is slightly different w.r.t. that both the format and arguments are constant and could be evaluated during compile time (ie. effectively constexpr) but because the fmt is passed as a function parameter it is not detected as strictly constexpr.

    Isn't there a way to have a fall-back in case the strict compile-time evaluation fails. In the example above, the function err_format cannot be constexpr because std::string does not allow constexpr allocation/initialisation across function boundaries.

    Is the behaviour you quoted compatible with the defined behaviour for std::format(...)?

  3. vitaut commented on May 19, 2022

    @vitaut
    Contributor

    To make this work (with or without strict) you'll need to pass the format string as fmt::format_string:

    template<typename... T>
    std::string err_format(fmt::format_string<T...> fmt, T&&... errorArgs) {
      return fmt::format(fmt, std::forward<T>(errorArgs)...);
    }

    Is the behaviour you quoted compatible with the defined behaviour for std::format(...)?

    Yes.

  4. RalphSteinhagen commented on May 19, 2022

    @RalphSteinhagen
    Author

    @vitaut thanks for the help and work-arounds! Much appreciated! πŸ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions