Given the maximum possible value, how to simply express the space needed to write such number in decimal form as text ?
The real task: logging process ids (pid_t
) with fixed length, using gcc on Linux. It'd be good to have a compile time expression to be used in the std::setw()
iomanipulator.
I have found that linux/threads.h header contains a PID_MAX
value with the maximum pid allocated to a process. So having
#define LENGTH(t) sizeof(#t)-1
the LENGTH(PID_MAX)
would be a compile time expression, but unfortunatelly this number is defined in hexa:
#define PID_MAX 0x8000
My current best solution is a bit oddish
static_cast<int>( ::floor( ::log(PID_MAX)/::log(10) + 1 ) );
But this is calculated runtime and uses functions from math.h