# std::optional<T>::reset

> C++ Atlas: `cppatlas.com/w/cpp/utility/optional/reset` | Source: https://en.cppreference.com/w/cpp/utility/optional/reset | Domain: Utility | Standards: C++17, C++20

If *this contains a value, destroy that value as if by value().T::~T(). Otherwise, there are no effects.

## Official Specification Summary (cppreference)

```cpp
void reset() noexcept; // (since C++17) (constexpr since C++20)
```

If `*this` contains a value, destroy that value as if by `value().T::~T()`. Otherwise, there are no effects.

`*this` does not contain a value after this call.

### Notes

| Feature-test macro | Value | Std | Feature |
| --- | --- | --- | --- |
| `__cpp_lib_optional` | `202106L` | (C++20) (DR20) | Fully `constexpr` |

### Example

```cpp
#include <iostream>
#include <optional>

struct A
{
    std::string s;
    A(std::string str) : s(std::move(str)) { std::cout << " constructed\n"; }
    ~A() { std::cout << " destructed\n"; }
    A(const A& o) : s(o.s) { std::cout << " copy constructed\n"; }
    A(A&& o) : s(std::move(o.s)) { std::cout << " move constructed\n"; }

    A& operator=(const A& other)
    {
        s = other.s;
        std::cout << " copy assigned\n";
        return *this;
    }

    A& operator=(A&& other)
    {
        s = std::move(other.s);
        std::cout << " move assigned\n";
        return *this;
    }
};

int main()
{
    std::cout << "Create empty optional:\n";
    std::optional<A> opt;

    std::cout << "Construct and assign value:\n";
    opt = A("Lorem ipsum dolor sit amet, consectetur adipiscing elit nec.");

    std::cout << "Reset optional:\n";
    opt.reset();
    std::cout << "End example\n";
}
```

Output:

```text
Create empty optional:
Construct and assign value:
 constructed
 move constructed
 destructed
Reset optional:
 destructed
End example
```

### Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

| DR | Applied to | Behavior as published | Correct behavior |
| --- | --- | --- | --- |
| P2231R1 | C++20 | `reset` was not constexpr while non-trivial destruction is allowed in `constexpr` in C++20 | made `constexpr` |

### See also

- [`operator=`](https://en.cppreference.com/w/cpp/utility/optional/operator=): assigns contents (public member function)

---
Attribution: content from https://en.cppreference.com/w/cpp/utility/optional/reset (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.