CKTIS / C++
std::nullopt — explicit absence of a value
Use `std::nullopt` to return or set an empty `std::optional` without confusing a missing result with a valid value such as zero.
Version: ISO C++23; implementation support may vary
Syntax
#include <optional>
std::optional<int> no_result() {
return std::nullopt;
}
void clear(std::optional<int>& result) {
result = std::nullopt;
}
bool is_empty(const std::optional<int>& result) {
return result == std::nullopt;
}std::nullopt is an inline constexpr constant of type std::nullopt_t from <optional>, available since C++17. The constructor that takes this tag creates an empty std::optional<T> without constructing a T. Use return std::nullopt; when a function returns std::optional<T> and there is no result. The constant itself does not say why the result is missing.
o == std::nullopt and !o.has_value() test for absence. if (o) tests presence, not contents: a stored 0, false, or nullptr is still a value. Use *o and o-> only when a value is present; in C++23, using either operator on an empty optional has undefined behavior, whereas o.value() throws std::bad_optional_access. Both o = std::nullopt and o.reset() destroy any contained value; do not use pointers or references to that value afterward.
In lesson 51, std::numeric_limits<int> from <limits> supplies the bounds of int: max() is the largest and min() the smallest representable value. For floating-point types with subnormal values, min() is the smallest positive normalized value; lowest() gives the lowest finite value. The example checks the bound appropriate to the sign of b before evaluating a + b. Short-circuiting && keeps the bound subtractions themselves in range. An out-of-range int sum would have undefined behavior.
In lesson 48, std::filesystem::perms from <filesystem> is a scoped enumeration used as a file-permission bitmask. A present std::filesystem::perms::none means no permission bits are set; an empty std::optional<std::filesystem::perms> means there is no result. This is context only: the example does not use this type or check or enforce permissions.
Examples
#include <iostream>
#include <limits>
#include <optional>
std::optional<int> checked_add(int a, int b) {
using Limits = std::numeric_limits<int>;
if ((b > 0 && a > Limits::max() - b) ||
(b < 0 && a < Limits::min() - b)) {
return std::nullopt;
}
return a + b;
}
int main() {
const auto print = [](std::optional<int> result) {
if (result) {
std::cout << *result << '\n';
} else {
std::cout << "no result\n";
}
};
print(checked_add(7, -7));
print(checked_add(std::numeric_limits<int>::max(), 1));
print(checked_add(std::numeric_limits<int>::min(), -1));
}Result
0 no result no result
The first call prints `0`; the next two each print `no result` because the sum is outside the range of `int`.
Related lessons
- C++ · Lesson 48 · Advanced · Version: 3
- C++ · Lesson 51 · Advanced · Version: 3
Sources
- No-value state indicator [optional.nullopt]
- Changes between C++14 and C++17 (P0636R0)
- Constructors [optional.ctor]
- Assignment [optional.assign]
- Comparison with nullopt [optional.nullops]
- Observers [optional.observe]
- Modifiers [optional.mod]
- numeric_limits members [numeric.limits.members]
- Preamble [expr.pre]
- Logical AND operator [expr.log.and]
- Enum class perms [fs.enum.perms]