Unfortunately, this is UB, if I interpret the wording correct (in any case, it’s not allowed):
§21.4.5 [string.access] p2
Returns:
*(begin() + pos)
ifpos < size()
, otherwise a reference to an object of typeT
with valuecharT()
; the referenced value shall not be modified.
(Editorial error that it says T
not charT
.)
.data()
and .c_str()
basically point back to operator[]
(§21.4.7.1 [string.accessors] p1
):
Returns: A pointer
p
such thatp + i == &operator[](i)
for eachi
in[0,size()]
.
Related Contents:
- Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++14?
- A positive lambda: ‘+[]{}’ – What sorcery is this? [duplicate]
- Optimizing away a “while(1);” in C++0x
- Legality of COW std::string implementation in C++11
- C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?
- reinterpret_cast creating a trivially default-constructible object
- Will std::string always be null-terminated in C++11?
- What exactly is the “immediate context” mentioned in the C++11 Standard for which SFINAE applies?
- Is it a conforming compiler extension to treat non-constexpr standard library functions as constexpr?
- Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable?
- Is the size of std::array defined by standard
- Capturing a reference by reference in a C++11 lambda
- c++ access static members using null pointer
- When do extra parentheses have an effect, other than on operator precedence?
- Does this code from “The C++ Programming Language” 4th edition section 36.3.6 have well-defined behavior?
- Is it possible to use std::string in a constexpr?
- When is a private constructor not a private constructor?
- Is a moved-from vector always empty?
- Matching alias template as template argument
- Can C++ code be valid in both C++03 and C++11 but do different things?
- How can an incomplete type be used as a template parameter to vector here?
- What is the value category of the operands of C++ operators when unspecified?
- Lambda capture and parameter with same name – who shadows the other? (clang vs gcc)
- In C++11, does `i += ++i + 1` exhibit undefined behavior?
- Why is modifying a string through a retrieved pointer to its data not allowed?
- Are notes and examples in the core language specification of the C++ Standard non-normative?
- What is the difference between C++03 `throw()` specifier and C++11 `noexcept`?
- Is std::string ref-counted in GCC 4.x / C++11?
- Do I really need to implement user-provided constructor for const objects?
- Using std::bind with member function, use object pointer or not for this argument?
- Is it legal to declare a constexpr initializer_list object?
- Is std::abs(0u) ill-formed?
- Is a constexpr array necessarily odr-used when subscripted?
- Why does the standard differentiate between direct-list-initialization and copy-list-initialization?
- So why is i = ++i + 1 well-defined in C++11?
- Is the Empty Base Class Optimization now a mandatory optimization (at least for standard-layout classes)?
- Is left-shifting (
- What is the purpose of a declaration like int (x); or int (x) = 10;
- Why is the std::initializer_list constructor preferred when using a braced initializer list?
- Is it defined behavior to reference an early member from a later member expression during aggregate initialization?
- What does the void() in decltype(void()) mean exactly?
- Extending temporary’s lifetime through rvalue data-member works with aggregate, but not with constructor, why?
- Can the point-of-instantiation be delayed until the end of the translation unit?
- Why does std::vector work with incomplete types in class definitions?
- Can using a lambda in header files violate the ODR?
- Brace elision in std::array initialization
- Is writing to &str[0] buffer (of a std:string) well-defined behaviour in C++11?
- How std::bind works with member functions
- Range based for-loop on array passed to non-main function
- How can I have multiple parameter packs in a variadic template?