How can I convert std::thread::id which is a class into a std::string? If I print std::thread::id using std::cout, I could see an integer getting printed, and this is possible because of << operator overloading.
#include <iostream>
#include <thread>
int main() {
std::cout << std::to_string(std::this_thread::get_id()) << std::endl;
}
With C++23, there is formatting support for thread::id, so you can do this:
#include <format>
#include <iostream>
#include <thread>
int main()
{
std::string s = std::format("{}", std::this_thread::get_id());
std::cout << s << "\n";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With