Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

std::format will not compile a format string it cannot check

Your error

error: call to consteval function 'basic_format_string<char, char [18]>::basic_format_string<5>' is not a constant expression2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The format string is only known at run time

If the pattern is fixed, make it constexpr so the compiler can read it.

Fix
#include <format>
#include <string>

constexpr std::string_view fixed = "id={} name={}";
std::string a = std::format(fixed, 7, "box");          // checked at compile time

std::string pattern = load_pattern_from_config();      // run time only
int count = 3;
std::string b = std::vformat(pattern, std::make_format_args(count));       // C++20
std::string c = std::format(std::runtime_format(pattern), count);          // C++26
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Since C++20, std::format checks the format string at compile time in the consteval constructor of std::basic_format_string. The error call to consteval function ... is not a constant expression means that check could not run or failed: the string is a run-time variable, a wrapper passed it on as string_view, or it does not match the arguments.

  • Applies to GCC 16.2 with libstdc++ (-std=c++20, -std=c++26), C++20 std::format with P2216R3 checks, C++26 std::runtime_format (GCC 14 and later)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Look at the first argument of the std::format call the compiler points to. A variable that is not constexpr, or a function parameter, means cause 1 or 2. A string literal written right there means cause 3: count the placeholders and check each type.

Run this first
g++ -std=c++20 -c main.cpp 2>&1 | grep -m3 -B2 -A6 "consteval"

Then find the line you see in this list and open its fix:

  1. 1The format string is only known at run timeThe first argument is a std::string, a char* buffer or a value read from a file, config or translation table
  2. 2A wrapper function passes its format string on as a plain stringThe error points inside your own log() or print helper, where a const std::string&, std::string_view or const char* parameter goes into std::format
  3. 3The literal does not match the argumentsThe format string is a literal at the call site, and the notes mention an invalid type specifier, an argument id, or that std::formatter must be specialized

What does the error look like?

  • error: call to consteval function 'basic_format_string<char, char [18]>::basic_format_string<5>' is not a constant expression
  • error: call to consteval function
  • std::formatter must be specialized

Cause 1 of 3

The format string is only known at run time

You seeThe first argument is a std::string, a char* buffer or a value read from a file, config or translation table

Check

A run-time string gives call to consteval function ... is not a constant expression because P2216R3 moved format string checks into a consteval constructor, and a value that only exists at run time cannot be checked there. The same paper keeps std::vformat for strings that really come from outside the program.

Check
grep -n "std::format(" src/*.cpp

Fix

If the pattern is fixed, make it constexpr so the compiler can read it. If it truly arrives at run time, call std::vformat with std::make_format_args, passing named variables rather than temporaries. In C++26, std::runtime_format says the same thing more plainly, and GCC ships it from version 14 with -std=c++26.

Fix
#include <format>
#include <string>

constexpr std::string_view fixed = "id={} name={}";
std::string a = std::format(fixed, 7, "box");          // checked at compile time

std::string pattern = load_pattern_from_config();      // run time only
int count = 3;
std::string b = std::vformat(pattern, std::make_format_args(count));       // C++20
std::string c = std::format(std::runtime_format(pattern), count);          // C++26

Verify

The file compiles. A bad run-time pattern now throws std::format_error when the line runs, so wrap the call in try/catch if the pattern comes from users.

Verify
g++ -std=c++26 -Wall main.cpp -o app && ./app

Confirmed by open-std.org

Did this fix it?

Cause 2 of 3

A wrapper function passes its format string on as a plain string

You seeThe error points inside your own log() or print helper, where a const std::string&, std::string_view or const char* parameter goes into std::format

Check

Inside a wrapper, the format parameter is an ordinary function argument, so the compile-time check fails there even though every caller passed a literal. P2508R1 made std::format_string<Args...> a public name for exactly this case: the wrapper takes it as its parameter, and the check then runs at each call site.

Check
grep -nE "std::(string_view|string const&|string&) (fmt|format)" src/*.hpp src/*.cpp

Fix

Change the wrapper's parameter type to std::format_string<Args...> and forward the arguments. Callers keep passing literals and get errors at their own line when a string is wrong.

Fix
#include <format>
#include <utility>

template <class... Args>
void log_line(std::format_string<Args...> fmt, Args&&... args) {
    write_raw(std::format(fmt, std::forward<Args>(args)...));
}

log_line("user {} logged in", user_id);

Verify

Calls with correct literals compile, and a deliberately wrong one such as log_line("{} {}", 1) fails at the call site instead of inside the helper.

Verify
g++ -std=c++20 -c logger.cpp

Confirmed by open-std.org

Did this fix it?

Cause 3 of 3

The literal does not match the arguments

You seeThe format string is a literal at the call site, and the notes mention an invalid type specifier, an argument id, or that std::formatter must be specialized

Check

A literal that disagrees with its arguments is the case P2216R3 set out to catch: std::format("{:d}", "I am not a number") used to throw format_error and is now ill-formed. libstdc++'s own tests expect the same call to consteval function error for an argument index with no argument behind it, and a std::formatter must be specialized error for a type that has no formatter.

Check
grep -n "std::format(" src/*.cpp

Fix

Make the specifiers fit the types, keep the number of {} placeholders or indexes within the arguments you pass, and give your own types a std::formatter specialization or format a member that already has one.

Fix
std::string ok1 = std::format("{}", "I am not a number");   // no :d for text
std::string ok2 = std::format("{} of {}", done, total);      // two fields, two args
std::string ok3 = std::format("point {}", p.to_string());     // or specialize std::formatter<Point>

Verify

The translation unit compiles with no consteval note, and the output strings read as expected when the program runs.

Verify
g++ -std=c++20 -Wall main.cpp -o app && ./app

Confirmed by open-std.org

Did this fix it?

How do I stop it coming back?

  • Accept std::format_string<Args...> in every logging or printing helper you write, never std::string_view.
  • Keep fixed patterns in constexpr std::string_view constants so they stay checkable when moved out of the call.
  • Route every run-time pattern through one function that uses std::runtime_format or std::vformat and catches std::format_error.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.