std::shared...::operator[](3)
β¨ 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 isnullor ifidxis 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms