Consider the following from C++11:
[C++11: 21.4.5]:
basic_string
element access [string.access]const_reference operator[](size_type pos) const; reference operator[](size_type pos);
1 Requires:
pos <= size()
.2 Returns:
*(begin() + pos)
ifpos < size()
, otherwise a reference to an object of typeT
with valuecharT()
; the referenced value shall not be modified.3 Throws: Nothing.
4 Complexity: constant time.
This means either:
pos == size()
case shall not be modified, orop[]
shall not be modified, even for the non-const
overload.The second scenario seems completely ridiculous, but I think it's what the wording most strongly implies.
Can we modify what we get from std::string::op[]
, or not? And is this not rather ambiguous wording?
Strings and String Manipulation in C++ C++ provides convenient and powerful tools to manipulate strings.
std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.
Yes, std::string (since C++11) is able to be moved i.e. it supports move semantics.
There is no functionality difference between string and std::string because they're the same type. That said, there are times where you would prefer std::string over string .
The quote means that you cannot modify the return of operator[]( size() )
, even if the value is well defined. That is, you must not modify the NUL terminator in the string even through the non-const overload.
This is basically your first option: i.e. pos >= size()
, but because of the requirement pos <= size()
the only possible value for that condition is pos == size()
.
The actual English description of the clause can be ambiguous (at least to me), but Appendix C, and in particular C.2.11 deals with changes in semantics in the string library, and there is no mention to this change --that would break user code. In C++03 the "referenced value shall not be modified" bit is not present and there is no ambiguity. The lack of mention in C.2.11 is not normative, but can be used as a hint that when they wrote the standard there was no intention on changing this particular behavior.
In n3690 (C++14 draft), the wording has been changed to:
Returns:
*(begin() + pos)
ifpos < size()
. Otherwise, returns a reference to an object of typecharT
with valuecharT()
, where modifying the object leads to undefined behavior.
I believe that this resolves the English ambiguity, and makes clear the intent of the original, ambiguous C++11 passage.
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