正常
std::optional (C++17)
In a Nutshell
A container for representing "a value that may not exist" — safer and more intuitive than returning a bool plus a pointer or an output parameter.
Header
#include <optional>
Core API Cheat Sheet
| Operation | Signature | Description |
|---|---|---|
| Construct | optional() | Default construction; contains no value |
| Assign empty | optional& operator=(nullopt_t) | Sets the state to no value |
| Check for value | explicit operator bool() const | Returns true when a value is present |
| Check for value | bool has_value() const | Same as above |
| Access value | T& operator*() | Dereferences to obtain the value (undefined behavior when there is no value) |
| Safe access | T& value() | Returns the value; throws bad_optional_access if there is none |
| Value or default | T value_or(const T& default_value) const | Returns the value if present, otherwise the default value |
| In-place construct | T& emplace(Args&&... args) | Constructs the value in place |
| Reset | void reset() noexcept | Destroys the contained value |
Minimal Example
C++
#include <iostream>
#include <optional>
#include <string>
std::optional<std::string> find(bool b) {
return b ? std::optional<std::string>{"found"} : std::nullopt;
}
int main() {
auto res = find(false);
std::cout << res.value_or("not found") << '\n';
if (auto val = find(true))
std::cout << *val << '\n';
}Embedded Applicability: High
- Zero-overhead abstraction: the no-value state takes up only a
bool-sized amount of storage, and no heap allocation is involved - Can replace raw pointers as the return type of functions that might fail, avoiding the risk of dereferencing a null pointer
- Fully supported since C++17; member functions become fully
constexprfrom C++23 onward, further widening the range of applicable scenarios
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| TBD | TBD | TBD |
See Also
Some content is adapted from cppreference.com, used under the CC-BY-SA 4.0 license