According to the C++23 standard, what is the output of this program?
#include <string>
#include <iostream>
int main() {
using namespace std::string_literals;
std::string s1("hello world",5);
std::string s2("hello world"s,5);
std::cout << s1 << s2;
}The meaning of the second argument is different for the two constructors.
hello world
Even though these constructor invocations look strikingly similar, the one that takes a C-style string and the one that takes a basic_string behave differently with respect to the second argument. For the former, it means "until this position", but for the latter it means "from this position".
In the constructor taking a C-style string (the s1 case) the second argument is treated as how many characters to take from the start of the C-style string. §[string.cons]¶14:
constexpr basic_string(const charT* s, size_type n, const Allocator& a = Allocator());Effects: Constructs an object whose initial value is the range
[s, s + n).
On the other hand, in the constructor that takes basic_string (the s2 case) the second argument is treated as the position to start copying the string from. §[string.cons]¶5:
constexpr basic_string(const basic_string& str, size_type pos, const Allocator& a = Allocator());(...)
Let
—
sbe the value ofstrprior to this call and—
rlenbepos + min(n, s.size() - pos)for the overloads with parametern, ands.size()otherwise.Effects: Constructs an object whose initial value is the range
[s.data() + pos, s.data() + rlen).