std::unique_ptr::operator* - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::unique_ptr::operator*

From cppreference.com
< cpp‎ | memory‎ | unique ptr

typename std::add_lvalue_reference<T>::type operator*() const; | (1) | (since C++11)
pointer operator->() const; | (2) | (since C++11)

`operator*` and `operator->` provide access to the object owned by *this.

The behavior is undefined if get() == nullptr

### [edit] Parameters

(none)

### [edit] Return value

1) Returns the object owned by *this, equivalent to *get().

2) Returns a pointer to the object owned by *this, i.e. get().

### [edit] Exceptions

1) may throw, e.g. if `pointer` defines a throwing `operator*`

2)
`noexcept` specification:
noexcept

### [edit] Example

Run this code

    #include <iostream>
    #include <memory>

    struct Foo {
        void bar() { std::cout << "Foo::bar\n"; }
    };

    void f(const Foo& foo)
    {
        std::cout << "f(const Foo&)\n";
    }

    int main()
    {
        std::unique_ptr<Foo> ptr(new Foo);

        ptr->bar();
        f(*ptr);
    }

Output:

    Foo::bar
    f(const Foo&)

### [edit] See also

get | returns a pointer to the managed object (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/unique_ptr/operator*&oldid=91435"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template