std::shared_ptr<T>::operator*, std::shared_ptr<T>::operator->
T& operator*() const noexcept; | (1) |
(since C++11) (constexpr since C++26) |
T* operator->() const noexcept; | (2) |
(since C++11) (constexpr since C++26) |
Dereferences the stored pointer.
When T is (possibly cv-qualified) void, it is unspecified whether operator* is declared. If it is declared, it is unspecified what its return type is, except that the declaration is guaranteed to be well-formed.
| When | (since C++17) |
If the stored pointer is null, the behavior is undefined.
Return value
*get()
get()
Example
#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
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2572 | C++11 |
operator* was required to be declared if T is cv-qualified void | not required to be declared in this case |
See also
| returns the stored pointer (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/memory/shared_ptr/operator*