std::experimental::optional<T>::value - cppreference.com
来自cppreference.com
constexpr T& value() &; constexpr const T & value() const &; |
(1) | (库基础 TS) |
constexpr T&& value() &&; constexpr const T&& value() const &&; |
(2) | (库基础 TS) |
Returns the contained value.
1) 等价于 return bool(*this) ? *val : throw bad_optional_access();。
2) 等价于 return bool(*this) ? std::move(*val) : throw bad_optional_access();。
参数
(无)
返回值
到所含值的引用。
异常
如果 *this 不包含值则抛出 std::experimental::bad_optional_access。
注解
解引用运算符 operator*() 并不检查 optional 是否含有值,这比 value() 更高效。
示例
#include <experimental/optional> #include <iostream> int main() { std::experimental::optional<int> opt = {}; try { int n = opt.value(); } catch (const std::logic_error& e) { std::cout << e.what() << '\n'; } }
可能的输出:
optional<T>::value: not engaged