---
name: "std::optional"
section: "3cxx"
platform: "linux"
package: "libstdc++-12-doc"
version: "12.4.0-5"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/std::optional"
---

# std::optional(3cxx)

Class template for optional values.

## NAME

std::optional\< \_Tp \> - Class template for optional values.

## SYNOPSIS

`#include <optional>`

Inherits **std::\_Optional\_base\< \_Tp, bool, bool \>**, and \_Enable\_copy\_move\< is\_copy\_constructible\_v\< \_Tp \>, \_\_and\_v\< is\_copy\_constructible\< \_Tp \>, is\_copy\_assignable\< \_Tp \> \>, is\_move\_constructible\_v\< \_Tp \>, \_\_and\_v\< is\_move\_constructible\< \_Tp \>, is\_move\_assignable\< \_Tp \> \>, optional\< \_Tp \> \>.

Inherited by std::ranges::\_\_detail::\_\_box\< \_Pred \>, std::ranges::\_\_detail::\_\_box\< \_Fp \>, and std::ranges::\_\_detail::\_\_box\< \_Tp \>.

### Public Types

**using** **value\_type** = \_Tp\

### Public Member Functions

template\<**typename** \_Up  = \_Tp, **\_Requires**\< **\_\_not\_self**\< \_Up \>, **\_\_not\_tag**\< \_Up \>, **is\_constructible**\< \_Tp, \_Up \>, **is\_convertible**\< \_Up, \_Tp \> \>  = true\> constexpr **optional** (\_Up &&\_\_t) noexcept(is\_nothrow\_constructible\_v\< \_Tp, \_Up \>)\
 template\<**typename** \_Up  = \_Tp, **\_Requires**\< **\_\_not\_self**\< \_Up \>, **\_\_not\_tag**\< \_Up \>, **is\_constructible**\< \_Tp, \_Up \>, \_\_not\_\< **is\_convertible**\< \_Up, \_Tp \> \> \>  = false\> constexpr **optional** (\_Up &&\_\_t) noexcept(is\_nothrow\_constructible\_v\< \_Tp, \_Up \>)\
 template\<**typename** \_Up , **\_Requires**\< \_\_not\_\< **is\_same**\< \_Tp, \_Up \> \>, **is\_constructible**\< \_Tp, const \_Up & \>, **is\_convertible**\< const \_Up &, \_Tp \>, \_\_not\_\< **\_\_converts\_from\_optional**\< \_Tp, \_Up \> \> \>  = true\> constexpr **optional** (const **optional**\< \_Up \> &\_\_t) noexcept(is\_nothrow\_constructible\_v\< \_Tp, const \_Up & \>)\
 template\<**typename** \_Up , **\_Requires**\< \_\_not\_\< **is\_same**\< \_Tp, \_Up \> \>, **is\_constructible**\< \_Tp, const \_Up & \>, \_\_not\_\< **is\_convertible**\< const \_Up &, \_Tp \> \>, \_\_not\_\< **\_\_converts\_from\_optional**\< \_Tp, \_Up \> \> \>  = false\> constexpr **optional** (const **optional**\< \_Up \> &\_\_t) noexcept(is\_nothrow\_constructible\_v\< \_Tp, const \_Up & \>)\
 template\<typename... \_Args, **\_Requires**\< **is\_constructible**\< \_Tp, \_Args... \> \>  = false\> constexpr **optional** (in\_place\_t, **\_Args** &&... **\_\_args**) noexcept(is\_nothrow\_constructible\_v\< \_Tp, \_Args... \>)\
 template\<**typename** \_Up , typename... \_Args, **\_Requires**\< **is\_constructible**\< \_Tp, **initializer\_list**\< \_Up \> &, \_Args... \> \>  = false\> constexpr **optional** (in\_place\_t, **initializer\_list**\< \_Up \> **\_\_il**, **\_Args** &&... **\_\_args**) noexcept(is\_nothrow\_constructible\_v\< \_Tp, **initializer\_list**\< \_Up \> &, \_Args... \>)\
 constexpr **optional** (**nullopt\_t**) noexcept\
 template\<**typename** \_Up , **\_Requires**\< \_\_not\_\< **is\_same**\< \_Tp, \_Up \> \>, **is\_constructible**\< \_Tp, \_Up \>, **is\_convertible**\< \_Up, \_Tp \>, \_\_not\_\< **\_\_converts\_from\_optional**\< \_Tp, \_Up \> \> \>  = true\> constexpr **optional** (**optional**\< \_Up \> &&\_\_t) noexcept(is\_nothrow\_constructible\_v\< \_Tp, \_Up \>)\
 template\<**typename** \_Up , **\_Requires**\< \_\_not\_\< **is\_same**\< \_Tp, \_Up \> \>, **is\_constructible**\< \_Tp, \_Up \>, \_\_not\_\< **is\_convertible**\< \_Up, \_Tp \> \>, \_\_not\_\< **\_\_converts\_from\_optional**\< \_Tp, \_Up \> \> \>  = false\> constexpr **optional** (**optional**\< \_Up \> &&\_\_t) noexcept(is\_nothrow\_constructible\_v\< \_Tp, \_Up \>)\
 template\<typename... \_Args\> constexpr **enable\_if\_t**\< is\_constructible\_v\< \_Tp, \_Args... \>, \_Tp & \> **emplace** (**\_Args** &&... **\_\_args**) noexcept(is\_nothrow\_constructible\_v\< \_Tp, \_Args... \>)\
 template\<**typename** \_Up , typename... \_Args\> constexpr **enable\_if\_t**\< is\_constructible\_v\< \_Tp, **initializer\_list**\< \_Up \> &, \_Args... \>, \_Tp & \> **emplace** (**initializer\_list**\< \_Up \> **\_\_il**, **\_Args** &&... **\_\_args**) noexcept(is\_nothrow\_constructible\_v\< \_Tp, **initializer\_list**\< \_Up \> &, \_Args... \>)\
 constexpr bool **has\_value** () const noexcept\
 constexpr **operator bool** () const noexcept\
 constexpr \_Tp && **operator\*** () &&noexcept\
 constexpr \_Tp & **operator\*** () &noexcept\
 constexpr const \_Tp && **operator\*** () const &&noexcept\
 constexpr const \_Tp & **operator\*** () const &noexcept\
 constexpr const \_Tp \* **operator-\>** () const noexcept\
 constexpr \_Tp \* **operator-\>** () noexcept\
 template\<**typename** \_Up  = \_Tp\> constexpr **enable\_if\_t**\< **\_\_and\_v**\< **\_\_not\_self**\< \_Up \>, \_\_not\_\< \_\_and\_\< **is\_scalar**\< \_Tp \>, **is\_same**\< \_Tp, **decay\_t**\< \_Up \> \> \> \>, **is\_constructible**\< \_Tp, \_Up \>, **is\_assignable**\< \_Tp &, \_Up \> \>, **optional** & \> **operator=** (\_Up &&**\_\_u**) noexcept(**\_\_and\_v**\< **is\_nothrow\_constructible**\< \_Tp, \_Up \>, **is\_nothrow\_assignable**\< \_Tp &, \_Up \> \>)\
 template\<**typename** \_Up \> constexpr **enable\_if\_t**\< **\_\_and\_v**\< \_\_not\_\< **is\_same**\< \_Tp, \_Up \> \>, **is\_constructible**\< \_Tp, const \_Up & \>, **is\_assignable**\< \_Tp &, const \_Up & \>, \_\_not\_\< **\_\_converts\_from\_optional**\< \_Tp, \_Up \> \>, \_\_not\_\< **\_\_assigns\_from\_optional**\< \_Tp, \_Up \> \> \>, **optional** & \> **operator=** (const **optional**\< \_Up \> &**\_\_u**) noexcept(**\_\_and\_v**\< **is\_nothrow\_constructible**\< \_Tp, const \_Up & \>, **is\_nothrow\_assignable**\< \_Tp &, const \_Up & \> \>)\
 constexpr **optional** & **operator=** (**nullopt\_t**) noexcept\
 template\<**typename** \_Up \> constexpr **enable\_if\_t**\< **\_\_and\_v**\< \_\_not\_\< **is\_same**\< \_Tp, \_Up \> \>, **is\_constructible**\< \_Tp, \_Up \>, **is\_assignable**\< \_Tp &, \_Up \>, \_\_not\_\< **\_\_converts\_from\_optional**\< \_Tp, \_Up \> \>, \_\_not\_\< **\_\_assigns\_from\_optional**\< \_Tp, \_Up \> \> \>, **optional** & \> **operator=** (**optional**\< \_Up \> &&**\_\_u**) noexcept(**\_\_and\_v**\< **is\_nothrow\_constructible**\< \_Tp, \_Up \>, **is\_nothrow\_assignable**\< \_Tp &, \_Up \> \>)\
 constexpr **void** **reset** () noexcept\
 constexpr **void** **swap** (**optional** &\_\_other) noexcept(is\_nothrow\_move\_constructible\_v\< \_Tp \> &&**is\_nothrow\_swappable\_v**\< \_Tp \>)\
 constexpr \_Tp & **value** () &\
 constexpr \_Tp && **value** () &&\
 constexpr const \_Tp & **value** () const &\
 constexpr const \_Tp && **value** () const &&\
 template\<**typename** \_Up \> constexpr \_Tp **value\_or** (\_Up &&**\_\_u**) &&\
 template\<**typename** \_Up \> constexpr \_Tp **value\_or** (\_Up &&**\_\_u**) const &\

## Detailed Description

### template\<typename \_Tp\>

class std::optional\< \_Tp \>"Class template for optional values.

## Author

Generated automatically by Doxygen for libstdc++ from the source code.
