Namespaces
Variants

std::optional<T>:: reset

From cppreference.net
Utilities library
void reset ( ) noexcept ;
(C++17부터)
(C++20부터 constexpr)

만약 * this 가 값을 포함하고 있다면, 해당 값을 value ( ) . T :: ~T ( ) 와 같은 방식으로 파괴합니다. 그렇지 않은 경우, 아무런 영향도 없습니다.

* this 는 이 호출 이후 값을 포함하지 않습니다.

목차

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_optional 202106L (C++20)
(DR20)
완전한 constexpr

예제

#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";
}

출력:

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

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
P2231R1 C++20 reset 함수가 constexpr이 아니었으나 C++20에서 비트리비얼 소멸이 constexpr 에서 허용됨 constexpr 로 변경됨

참고 항목

내용을 할당
(public member function)