3

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
cpplearner
  • 13,776
  • 2
  • 47
  • 72
GeePokey
  • 159
  • 7

2 Answers2

3

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 .

GeePokey
  • 159
  • 7
  • good answer because to tell you the truth, I had no idea what was going on other than the pointer was the problem (in my similar format code within a template) – ycomp Feb 06 '23 at 05:29
1

Formatting of non-void pointers is disallowed by default. You should wrap them in fmt::ptr or cast to void*:

std::string resultstring = std::format("{:p}", fmt::ptr(p));
vitaut
  • 49,672
  • 25
  • 199
  • 336
  • fmt::ptr() looks nice - and I would prefer that over a normal cast, but it is not in the standard library - only available if [fmt library](https://fmt.dev/latest/index.html) is installed, so I am accepting the (void *) answser. – GeePokey Oct 31 '22 at 03:32