Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::optional<T>::reset

      From cppreference.com
      <cpp‎ |utility‎ |optional
       
       
      Utilities library
       
       
      void reset()noexcept;
      (since C++17)
      (constexpr since C++20)

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

      *this does not contain a value after this call.

      Contents

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_optional202106L(C++20)
      (DR20)
      Fullyconstexpr

      [edit]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 destructedReset optional: destructedEnd example

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P2231R1C++20reset was not constexpr while non-trivial destruction is allowed inconstexpr in C++20madeconstexpr

      [edit]See also

      assigns contents
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/optional/reset&oldid=172858"

      [8]ページ先頭

      ©2009-2025 Movatter.jp