std::string doesn't exist at compile time. If you want to have such a behavior, you can use the string literals with constexpr as below:
constexpr const char* const var = "string";
To understand more about this, please see the assemble code generated for this:
#include <string>
int main()
{
constexpr const char* const str = "string";
const std::string test = "test";
}
With X86-64 Clang 6.0.0 compiler and with 0 optimizations,
constexpr const char* const str = "string";
generated below code:
subq $80, %rsp
movq $.L.str, -8(%rbp)
leaq -48(%rbp), %rax
.L.str:
.asciz "string"
and for const std::string test = "test"; below code is generated (Just a snippet)
So it calls the std::allocater which allocates the memory on the heap and then constructs the string object.
movq %rax, %rdi
movq %rax, -72(%rbp) # 8-byte Spill
callq std::allocator<char>::allocator() [complete object constructor]
movl $.L.str.1, %ecx
movl %ecx, %esi
leaq -40(%rbp), %rdi
movq -72(%rbp), %rdx # 8-byte Reload
callq std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
jmp .LBB0_1