Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::optional<T>::value_or

      From cppreference.com
      <cpp‎ |experimental‎ |optional
       
       
       
       
       
      template<class U>
      constexpr T value_or( U&& default_value)const&;
      (library fundamentals TS)
      template<class U>
      constexpr T value_or( U&& default_value)&&;
      (library fundamentals TS)

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

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

      Contents

      [edit]Parameters

      default_value - the value to use in case*this is empty
      Type requirements
      -
      T must meet the requirements ofCopyConstructible in order to use overload (1).
      -
      T must meet the requirements ofMoveConstructible in order to use overload (2).
      -
      U&& must be convertible toT.

      [edit]Return value

      The current value if*this has a value, ordefault_value otherwise.

      [edit]Exceptions

      Any exception thrown by the selected constructor of the return valueT.

      [edit]Example

      Run this code
      #include <cstdlib>#include <experimental/optional>#include <iostream> std::experimental::optional<constchar*> maybe_getenv(constchar* n){if(constchar* x=std::getenv(n))return x;elsereturn{};} int main(){std::cout<< maybe_getenv("MYPWD").value_or("(none)")<<'\n';}

      Possible output:

      (none)

      [edit]See also

      returns the contained value
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/experimental/optional/value_or&oldid=155045"

      [8]ページ先頭

      ©2009-2025 Movatter.jp