Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::variant<Types...>::~variant

      From cppreference.com
      <cpp‎ |utility‎ |variant
       
       
      Utilities library
       
       
      ~variant();
      (since C++17)
      (constexpr since C++20)

      Ifvalueless_by_exception() istrue, does nothing. Otherwise, destroys the currently contained object.

      This destructor is trivial ifstd::is_trivially_destructible_v<T_i> istrue for allT_i inTypes....

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_variant202106L(C++20)
      (DR)
      Fullyconstexprstd::variant

      [edit]Example

      Run this code
      #include <cstdio>#include <variant> int main(){struct X{ ~X(){ puts("X::~X();");}};struct Y{ ~Y(){ puts("Y::~Y();");}}; {        puts("entering block #1");std::variant<X,Y> var;        puts("leaving block #1");} {        puts("entering block #2");std::variant<X,Y> var{std::in_place_index_t<1>{}};// constructs var(Y)        puts("leaving block #2");}}

      Output:

      entering block #1leaving block #1X::~X();entering block #2leaving block #2Y::~Y();

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      P2231R1C++20the destructor was notconstexpr while non-trivial destructors can beconstexpr in C++20madeconstexpr
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/%7Evariant&oldid=172842"

      [8]ページ先頭

      ©2009-2025 Movatter.jp