std::shared...:operator->(3)
π
std::shared...:operator->(3) β C++ Standard Libary π
okay so welcome to the page for std::shared_ptrβs dereference besties π this one is about std::shared_ptr::operator* and std::shared_ptr::operator->, and yes, both of them are since C++11. letβs get into it, no cap π«Ά
β¨ NAME β¨
std::shared_ptr::operator*,std::shared_ptr::operator-> - std::shared_ptr::operator*,std::shared_ptr::operator->
πΈ Synopsis πΈ
both are const noexcept, and both are since C++11:
T& operator*() const noexcept; (1) (since C++11)
T* operator->() const noexcept; (2) (since C++11)
these bad boys dereference the stored pointer. the behavior is undefined if the stored pointer is null π not a vibe, not a maybe β actual undefined behavior, bestie. do not play with that.
π Parameters π
(none) β literally zero arguments. nothing. nada. the void is empty and we love that for them β¨
π Return value π
- the result of dereferencing the stored pointer, i.e.
*get(). - the stored pointer, i.e.
get().
β¨ Remarks β¨
- when
Tis an array type or (possibly cv-qualified) (since C++17)void, it is unspecified whether function (1) is declared. if it is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function shall be well formed. this makes it possible to instantiatestd::shared_ptr<void>π - when
Tis an array type, it is unspecified whether function (2) is declared. if it is declared, it is unspecified what its return type (since C++17) is, except that the declaration of the function shall be well-formed.
π Example π
run it: // Run this code
#include <iostream>
#include <memory>
struct Foo
{
Foo(int in) : a(in) {}
void print() const
{
std::cout << "a = " << a << '\n';
}
int a;
};
int main()
{
auto ptr = std::make_shared<Foo>(10);
ptr->print();
(*ptr).print();
}
π Output π
a = 10
a = 10
π See also π
getβ returns the stored pointer (public member function)
mwah π this oneβs from http://cppreference.com, dated 2024.06.10, and the page is std::shared...:operator->(3). periodt. π«Ά
π¦ 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