std::optional::reset(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ std::optional::reset(3) πŸŽ€

hey bestie πŸ’– welcome to the C++ Standard Libary manual β€” yes, β€œLibary” is exactly how the header spells it, and we are respecting the typo in this house 🫢✨. This page is all about std::optional::reset, aka the β€œbye, value” button for your optional. she’s lowkey iconic πŸ’….


✨ NAME ✨

std::optional::reset β€” std::optional::reset. That’s it. That’s the name. She said what she said πŸ’€.

πŸ“œ Synopsis πŸ“œ

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

Okay so here’s the tea β˜•: if *this contains a value, reset() destroys that value as if by value().T::~T(). Otherwise, there are no effects. No drama, no leftovers ✨. After this call, *this does not contain a value. That’s the whole plot β€” she clears the slot and leaves it empty, periodt πŸ’….

She’s been around since C++17 and got her constexpr glow-up since C++20, and the signature literally has noexcept on it. Slay 🫢.

πŸ’» Example πŸ’»

// Run this code

#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 πŸ“€

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

the leading spaces in that output are not a glitch, bestie β€” they’re part of the example strings, iykyk ✨. The whole story is giving β€œconstruct, move, destruct, reset, destruct again” and honestly? cinema πŸ“.

🩹 Defect reports 🩹

The following behavior-changing defect reports were applied retroactively to previously published C++ standards β€” historic glow-up behavior πŸ’…:

  • DR: P2231R1 β€” Applied to: C++20. Behavior as published: reset was not constexpr while non-trivial destruction is allowed in constexpr in C++20. Correct behavior: made constexpr.

πŸ‘€ See also πŸ‘€

  • operator= β€” assigns contents (public member function). She’s the other public member function who assigns contents, and she’s also invited to the group chat πŸ‘‘.

source/version: http://cppreference.com, dated 2024.06.10, for std::optional::reset(3) β€” that’s the whole footer, bestie, and she ate πŸ’–βœ¨πŸ«Ά

πŸ“¦ where this page comes from