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

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

✨ NAME ✨

okay so bestie, welcome to the manual page for std::shared...::operator[](3) β€” it lives in the C++ Standard Libary (yes, "Libary" is literally how it's spelled up there, we are not fixing the header's typo πŸ’…). The NAME section is doing that iconic double-tap thing:

  • std::shared_ptr::operator[] β€” std::shared_ptr::operator[]

πŸŽ€ Synopsis πŸŽ€

Here's the signature, bestie β€” note the (since C++17) glow-up:

element_type& operator[]( std::ptrdiff_t idx ) const;  (since C++17)

It indexes into the array pointed to by the stored pointer. Slay, but read the fine print.

⚠️ undefined behavior alert: the behavior is undefined if the stored pointer is null or if idx is negative. do not do this, iykyk 😭

If T (the template parameter of shared_ptr) is an array type U[N], idx must be less than N β€” otherwise the behavior is undefined. periodt, no cap πŸ’€


πŸ’œ Parameters πŸ’œ

  • idx β€” the array index

🌈 Return value 🌈

A reference to the idx-th element of the array, i.e., get()[idx]. literally that element, bestie ✨


πŸ”₯ Exceptions πŸ”₯

Throws nothing. not a single exception, she's quiet 🫢

πŸ¦‹ Remarks πŸ¦‹

When T is not an array type, it is unspecified whether this function is declared. If the function is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function is guaranteed to be legal. this is the "sorry bestie, it's unspecified" part πŸ™ˆ


🌸 Example 🌸

Run this code, bestie πŸ‘‡

        // Run this code

        #include <cstddef>
        #include <iostream>
        #include <memory>

        int main()
        {
            const std::size_t arr_size = 10;
            std::shared_ptr<int[]> pis(new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8,
       9});
            for (std::size_t i = 0; i < arr_size; ++i)
                std::cout << pis[i] << ' ';
            std::cout << '\n';
        }

Output:

0 1 2 3 4 5 6 7 8 9

πŸ‘‘ See also πŸ‘‘

  • get β€” returns the stored pointer (public member function)

and that's the whole vibe, bestie πŸ’– source/version/date shoutout: http://cppreference.com Β· 2024.06.10 Β· manual std::shared...::operator[](3) ✨🫢

πŸ“¦ where this page comes from