Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to printf() std::this_thread::get_id() in C++?

I've tried to cast to void* and use %p. I've also tried intptr_t with a format of %lx. Both times I get the "Invalid Cast" error.

I'm using -Wall -Werror which checks that the arguments of the printf() match the function's format string. So I can't merely count on get_id() leaving a 4- or 8-byte value on the stack and simply printing that value as hex.

I'm using gcc version 9.2.1 20190827 (Red Hat 9.2.1-1) (GCC)

The OS is Fedora release 31 (Thirty One) . The CPU is 64-bit Intel x86.

like image 673
Swiss Frank Avatar asked Sep 05 '26 00:09

Swiss Frank


1 Answers

Use streams:

std::ostringstream oss;
oss << std::this_thread::get_id() << std::endl;
printf("%s\n", oss.str().c_str());
like image 170
rafix07 Avatar answered Sep 07 '26 22:09

rafix07