In case of std::string, if we access an element where (element position) == (size of string)
the standard says that it returns a reference to an object of type charT
with value charT()
.
const_reference operator[](size_type pos) const; reference operator[](size_type pos);
Expects: pos <= size().
Returns: *(begin() + pos) if pos < size(). Otherwise, returns a reference to an object of type charT with value charT(), where modifying the object to any value other than charT() leads to undefined behavior.
http://eel.is/c++draft/strings#string.access-1
Unfortunately I couldn't reason about this, it would have been better if it has been Undefined Behavior.
Can somebody explain the rationale behind this?
Substring in C++ A substring is a part of a string. A function to obtain a substring in C++ is substr(). This function contains two parameters: pos and len. The pos parameter specifies the start position of the substring and len denotes the number of characters in a substring.
std::string actually maintains the size as one of its data member.
String Indexing Individual characters in a string can be accessed by specifying the string name followed by a number in square brackets ( [] ). String indexing in Python is zero-based: the first character in the string has index 0 , the next has index 1 , and so on.
A substring is a sequence of consecutive characters from a string. For example, “with Educative” is a substring of “Learn C++ with Educative”. The function will return a substring from the character of the pos index (inclusive) up to the character with the ( pos + len - 1) index.
You have to consider the full specs.
First of all:
Expects: pos <= size().
If you dont follow the precondition you have undefined behaviour anyhow. Now...
Returns: *(begin() + pos) if pos < size(). Otherwise, returns a reference to an object of type charT with value charT(), where modifying the object to any value other than charT() leads to undefined behavior.
The only (valid) case that "otherwise" refers to is when pos == size()
. And that is probably to emulate c string behaviour that have a some_string[size]
element that can be accessed. Note that charT()
is typically just '\0'
.
PS: One might think that to implement the specification, operator[]
would have to check if pos == size
. However, if the underlying character array has a charT()
at the end of the string, then you get the described behaviour basically for free. Hence, what seems a little different from "usual" access into an array is actually just that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With