6

Is it possible to output dynamic information to the compiler output during compilation?

A more concrete example (C++11):

static_assert(sizeof(A) == 4, "Wrong size of A");

I would like to output the value of sizeof(A) when the assertion fails (or even always), but the message in static_assert can't contain any dynamic information.

Is there any way to output the value of sizeof(A) to the compiler log?

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • Well, technically, `sizeof(A)` isn't particularly "dynamic", since it's known to the compiler. Right? – cadaniluk Nov 25 '15 at 16:01
  • If it really were dynamic information the compiler wouldn't know it. So no, the compiler can’t output dynamic information, but there are tricks to make it print the static size of a type. – idmean Nov 25 '15 at 16:07
  • There is a proposal to allow constant expression `char const*`s instead of just string literals for the static assertion error message. Once that's accepted, you can compose own error messages and include formatted numbers, e.g. via [Constainer's printf](https://github.com/Arcoth/Constainer/blob/master/StaticPrintf.hxx). – Columbo Nov 25 '15 at 16:19

0 Answers0