#include <iostream>
#include <string>
const char* foo(int value) {
thread_local std::string retval;
retval = std::to_string(value);
return retval.c_str(); // Pointer to inner buffer of 'std::string' obtained here
// Inner buffer of 'std::string' deallocated by call to destructor
// Inner pointer of container used after re/deallocation
}
int main(int argc, [[maybe_unused]] char* argv[]) {
std::cout << foo(argc) << '\n';
}
The thread_localstd::string is implicitly static so, the const char* will be valid for that thread until the next time the thread calls the function (and until the thread dies).
The
thread_local
std::string
is implicitlystatic
so, theconst char*
will be valid for that thread until the next time the thread calls the function (and until the thread dies).