Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::variant<Types...>::valueless_by_exception

      From cppreference.com
      <cpp‎ |utility‎ |variant
       
       
      Utilities library
       
       
      constexprbool valueless_by_exception()constnoexcept;
      (since C++17)

      Returnsfalse if and only if the variant holds a value.

      [edit]Notes

      A variant may become valueless when initializing the contained value in the following situations:

      • (guaranteed) an exception is thrown duringmove assignment
      • (optional) an exception is thrown duringcopy assignment
      • (optional) an exception is thrown during a type-changingassignment
      • (optional) an exception is thrown during a type-changingemplace

      Since variant is never permitted to allocate dynamic memory, the previous value cannot be retained and, therefore, restored in these situations. The "optional" cases can avoid throwing an exception if the type provides non-throwing moves and the implementation first constructs the new value on the stack and then moves it into the variant.

      This applies even to variants of non-class types:

      struct S{    operatorint(){throw42;}};std::variant<float,int> v{12.f};// OKv.emplace<1>(S());// v may be valueless

      A variant that isvalueless by exception — that is, has no value due to a previous exception from one of the situations listed above — is treated as being in an invalid state:

      [edit]Example

      Run this code
      #include <cassert>#include <iostream>#include <stdexcept>#include <string>#include <variant> struct Demo{    Demo(int){}    Demo(const Demo&){throwstd::domain_error("copy ctor");}    Demo& operator=(const Demo&)=default;}; int main(){std::variant<std::string, Demo> var{"str"};assert(var.index()==0);assert(std::get<0>(var)=="str");assert(var.valueless_by_exception()==false); try{        var= Demo{555};}catch(conststd::domain_error& ex){std::cout<<"1) Exception: "<< ex.what()<<'\n';}assert(var.index()==std::variant_npos);assert(var.valueless_by_exception()==true); // Now the var is "valueless" which is an invalid state caused// by an exception raised in the process of type-changing assignment. try{        std::get<1>(var);}catch(conststd::bad_variant_access& ex){std::cout<<"2) Exception: "<< ex.what()<<'\n';}     var="str2";assert(var.index()==0);assert(std::get<0>(var)=="str2");assert(var.valueless_by_exception()==false);}

      Possible output:

      1) Exception: copy ctor2) Exception: std::get: variant is valueless

      [edit]See also

      reads the value of the variant given the index or the type (if the type is unique), throws on error
      (function template)[edit]
      returns the zero-based index of the alternative held by thevariant
      (public member function)[edit]
      exception thrown on invalid accesses to the value of avariant
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/valueless_by_exception&oldid=180177"

      [8]ページ先頭

      ©2009-2025 Movatter.jp