Since the compiler is allowed to store the same string literal that is used elsewhere in one place, it is more than likely that both std::begin("testString")
and std::end("testString")
actually refer to the same same line.
This is not required to be done by standard, and the compiler can store both of these string literals in different places that would violate this code. Just because it works doesnβt mean you should do it. I would suggest using
std::vector<char> str; const char [] chstr = "testString" str.assign(std::begin(chstr), std::end(chstr));
Or better yet
std::string str = "testString"; copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout, " "));
source share