Namespaces
Variants

std::shared_ptr<T>::operator*, std::shared_ptr<T>::operator->

From cppreference.com
 
 
Memory management library
Allocators
Memory resources
Uninitialized storage (until C++20*)
Garbage collector support (until C++23)
 
 
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 T is an array type, it is unspecified whether any of operator* and operator-> is declared. If any of them is declared, it is unspecified what its return type is, except that the declaration is guaranteed to be well-formed.

(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:

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) [edit]