std::shared_ptr<T>::operator*, std::shared_ptr<T>::operator-> - cppreference.com
From cppreference.com
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
1) *get()
2) 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:
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) [edit] |