std::basic_...::operator[](3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::basic_...::operator[](3) πŸ’–

okay so bestie, this is the std::basic_...::operator[](3) page from the C++ Standard Libary manual, section 3, and it's literally all about std::basic_string::operator[]. no cap, we're getting into character-reference territory πŸ’…

✨ NAME ✨

std::basic_string::operator[] β€” yep, that's the whole name: std::basic_string::operator[]. iconic behavior, honestly πŸŽ€

🌸 Synopsis 🌸

CharT& operator[]( size_type pos );             (1) (constexpr since C++20)
const CharT& operator[]( size_type pos ) const; (2) (constexpr since C++20)

So here's the tea: this returns a reference to the character at the specified location pos if pos < size(), or a reference to CharT() if pos == size(). and bestie, hear me: no bounds checking is performed. none. zero. it's giving "you're on your own" 😭

If pos > size(), the behavior is undefined. like, not cute, not safe, just undefined fr fr.

For overload (1), if pos == size(), the behavior is undefined if the object referred by the returned reference is modified to any value other than CharT() (since C++11). so yeah, that's a very specific vibe check and you better respect it πŸ’€

πŸ¦‹ Parameters πŸ¦‹

  • pos β€” position of the character to return.

🌈 Return value 🌈

*(begin() + pos) if pos < size(), or a reference to CharT() if pos == size(). that's it, that's the whole return-value moment ✨

⭐ Complexity ⭐

Constant. constant time, no cap, we love a predictable icon πŸ‘‘

πŸ“ Example πŸ“

the example has a // Run this code invitation, and here's the code exactly:

#include <iostream>
#include <string>

int main()
{
    const std::string e("Exemplar");
    for (unsigned i = e.length() - 1; i != 0; i /= 2)
        std::cout << e[i];
    std::cout << '\n';

    const char* c = &e[0];
    std::cout << c << '\n'; // print as a C string

    // Change the last character of s into a 'y'
    std::string s("Exemplar ");
    s[s.size() - 1] = 'y'; // equivalent to s.back() = 'y';
    std::cout << s << '\n';
}

🐚 Output 🐚

rmx
Exemplar
Exemplary

πŸ’œ Defect reports πŸ’œ

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. history really said "let's fix that" and honestly? slay 🫢

  • LWG 259 β€” applied to C++98. As published: non-const overload could return const lvalue data()[pos], which was ill-formed. Correct behavior: changed to return *(begin() + pos).
  • LWG 2475 β€” applied to C++11. As published: if pos == size(), the behavior of modifying the object referred by the returned reference was undefined. Correct behavior: well-defined if modified to CharT().

πŸ”₯ See also πŸ”₯

  • at β€” accesses the specified character with bounds checking (public member function)
  • front β€” accesses the first character (DR*) (public member function)
  • back β€” accesses the last character (DR*) (public member function)
  • operator[] β€” accesses the specified character (public member function of std::basic_string_view<CharT,Traits>)

okay bestie, that's the whole page πŸ’– source/version is http://cppreference.com, dated 2024.06.10, and the page title is std::basic_...::operator[](3). go forth and index responsibly, and remember: no bounds checking means the bounds are not checking you back πŸ˜­πŸ’…

πŸ“¦ where this page comes from