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

FreeBSD Manual Pages

  
 
  

home | help
std::optional::value_or(3)    C++ Standard Libary   std::optional::value_or(3)

NAME
       std::optional::value_or - std::optional::value_or

Synopsis
	  template< class U >				    (1)	(since C++17)
	  constexpr T value_or(	U&& default_value ) const&;
	  template< class U >				    (2)	(since C++17)
	  constexpr T value_or(	U&& default_value ) &&;

	  Returns  the contained value if *this	has a value, otherwise returns
       default_value.

	  1) Equivalent	to bool(*this) ? **this	:
	  static_cast<T>(std::forward<U>(default_value))
	  2) Equivalent	to bool(*this) ? std::move(**this) :
	  static_cast<T>(std::forward<U>(default_value))

Parameters
	  default_value		-	  the value to use in  case  *this  is
       empty

Type requirements
	  -
	  T  must  meet	 the requirements of CopyConstructible in order	to use
       overload	(1).
	  -
	  T must meet the requirements of MoveConstructible in	order  to  use
       overload	(2).
	  -
	  U&& must be convertible to T

Return value
	  The current value if *this has a value, or default_value otherwise.

Exceptions
	  Any exception	thrown by the selected constructor of the return value
       T.

Example
       // Run this code

	#include <optional>
	#include <iostream>
	#include <cstdlib>

	std::optional<const char*> maybe_getenv(const char* n)
	{
	    if(const char* x = std::getenv(n))
	       return x;
	    else
	       return {};
	}
	int main()
	{
	     std::cout << maybe_getenv("MYPWD").value_or("(none)") << '\n';
	}

Possible output:
	(none)

See also
	  value	returns	the contained value
		(public	member function)

http://cppreference.com		  2022.07.31	    std::optional::value_or(3)

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

home | help