Question
How to print non-structural results of constexpr functions at compile time with clang++?
I'm writing a constexpr
code and I would like to inspect computed values at compile time.
The usual trick is to do something like this:
struct ReturnValue
{
int value1;
int value2;
};
constexpr ReturnValue doTheComputation()
{
return { .value1 = 10, .value2 = 20 };
}
template< auto t > struct p;
p< doTheComputation() > foo;
This gives a nice compile error:
implicit instantiation of undefined template 'p<ReturnValue{10, 20}>'
and I can see the value of the result.
However, this does not work for non-structural types, like std::string_view
, std::string
, or anything that resembles a string, as such types are not allowed to be NTTP.
Non-working example with std::string_view
: https://godbolt.org/z/da8W557nK
Non-working example with char const *
: https://godbolt.org/z/5Mvqfx95q
Even if I use char const[ some_large_numer ]
to ensure the message fits, like this, instead of a string, I get the ASCII values listed as template parameters:
implicit instantiation of undefined template 'p<ReturnValue{{115, 111, 109, 101, 32, 115, 101, 99, 114, 101, ...}}>
Are there any tips or tricks to print values of non-structural types at compile-time (as results of constexpr functions)?
I've seen an unofficial patch for GCC that apparently solves that (I haven't tested it), but I'm searching for a solution for clang (Apple-clang from Xcode 15 or newer and LLVM 18 or newer).