Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::varian...y_exception(3)  C++ Standard Libary std::varian...y_exception(3)

NAME
       std::variant::valueless_by_exception  -	std::variant::valueless_by_ex-
       ception

Synopsis
	  constexpr  bool  valueless_by_exception()  const  noexcept;	(since
       C++17)

	  Returns false	if and only if the variant holds a value.

Notes
	  A variant may	become valueless in the	following situations:

	    *  (guaranteed) an exception is thrown during the move initializa-
       tion of the
	      contained	value during move assignment
	    * (optionally) an exception	is thrown during the copy  initializa-
       tion of the
	      contained	value during copy assignment
	    *  (optionally)  an	exception is thrown when initializing the con-
       tained value during
	      a	type-changing assignment
	    * (optionally) an exception	is thrown when initializing  the  con-
       tained value during
	      a	type-changing emplace

	  Since	 variant is never permitted to allocate	dynamic	memory,	previ-
       ous value cannot
	  be retained in these situations. The situations marked  "optionally"
       can be worked
	  around  by implementations that first	construct the new value	on the
       stack and then
	  move it into the variant (provided non-throwing move).

	  This applies even to variants	of non-class types:

	struct S {
	    operator int() { throw 42; }
	};
	std::variant<float, int> v{12.f}; // OK
	v.emplace<1>(S()); // v	may be valueless

	  A variant that is valueless by exception is treated as being	in  an
       invalid state:
	  index	returns	variant_npos, get and visit throw bad_variant_access.

Example
       // Run this code

	#include <cassert>
	#include <iostream>
	#include <stdexcept>
	#include <string>
	#include <variant>

	struct Demo {
	    Demo(int) {}
	    Demo(const Demo&) {	throw std::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 (const std::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 assign-
       ment.

	    try	{
		std::get<1>(var);
	    } catch (const std::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 ctor
	2) Exception: std::get:	variant	is valueless

See also
	  std::get(std::variant) reads the value of the	variant	given the  in-
       dex or the type
	  (C++17)		 (if the type is unique), throws on error
				 (function template)
				 returns  the zero-based index of the alterna-
       tive held by the
	  index			 variant
				 (public member	function)
	  bad_variant_access	 exception thrown on invalid accesses  to  the
       value of	a
	  (C++17)		 variant
				 (class)

http://cppreference.com		  2022.07.31	  std::varian...y_exception(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::variant::valueless_by_exception&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help