Namespaces
Variants

std::optional<T>::reset

cppreference.net에서
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐지됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
Swap 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
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)