Search code examples
c++compiler-errorsc++20fmtstdformat

std::format pointer error C3615: consteval function 'std::_Compile_time_parse_format_specs' cannot result in a constant expression


How to fix (stdC++20 mode VS2022)

#include <format>
#include <string>

auto dump(int *p)
{
    std::string resultstring = std::format(" p points to address {:p}", p);

resulting in:

error C3615: consteval function 'std::_Compile_time_parse_format_specs' cannot result in a constant expression

Solution

  • Cast the pointer to void like this:

    std::string resultstring = std::format("{:p}", (void *)p);
    

    The problem is not with the format string itself, but rather that the templated type checking on the variable arguments FAILS for any non-void pointer types, generally with a confusing error in the header .