I was surprised to see that this code compiles:
std::string s2 = {"Hello", "World"};
Executing the statement causes a std::exception
with message basic_string::_S_create
but why does it compile at all?
This compiles std::string s1 = {"Hello"};
and seems to generate a normal string, while a longer initialization list does lead to a compilation error.
You can create a string form two input iterators (that should of course point to the same memory range):
template< class InputIt >
basic_string(InputIt first, InputIt last, const Allocator& alloc = Allocator());
In this case, I'd guess that both string literals are converted to const char*
which sis a valid input iterator, but as they don't belong to the same range, the constructor will sooner or later access an invalid memory location, when it tries to iterate from the first to the second.
I can't however reproduce your problem with the second example - that should be valid c++ code.