GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::optional::value(3) C++ Standard Libary std::optional::value(3)

std::optional::value - std::optional::value


constexpr T& value() &; (1) (since C++17)
constexpr const T& value() const &;
constexpr T&& value() &&; (2) (since C++17)
constexpr const T&& value() const &&;


If *this contains a value, returns a reference to the contained value.


Otherwise, throws a std::bad_optional_access exception.


(none)


A reference to the contained value.


std::bad_optional_access if *this does not contain a value.


The dereference operator operator*() does not check if this optional contains a
value, which may be more efficient than value().

// Run this code


#include <optional>
#include <iostream>
int main()
{
std::optional<int> opt = {};


try {
[[maybe_unused]] int n = opt.value();
} catch(const std::bad_optional_access& e) {
std::cout << e.what() << '\n';
}
try {
opt.value() = 42;
} catch(const std::bad_optional_access& e) {
std::cout << e.what() << '\n';
}


opt = 43;
std::cout << *opt << '\n';


opt.value() = 44;
std::cout << opt.value() << '\n';
}


bad optional access
bad optional access
43
44


returns the contained value if available, another value
value_or otherwise
(public member function)
operator-> accesses the contained value
operator* (public member function)
bad_optional_access exception indicating checked access to an optional that doesn't
(C++17) contain a value
(class)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.