Threads have their own stack, but implementing a thread-local heap is difficult. The compiler does not know how much to allocate on the stack for the string. You could put it on the top of the stack and increase the stack pointer when initialising, but what if you have two strings? Furthermore, std::string
is a datatype defined in the standard library, not the compiler, so its ability to reason about it is limited. I think it is sensible for a compiler to reject this code.