Files
esphome/esphome/core/optional.h
T
J. Nick Koston 3bca31ba9f Replace custom esphome::optional with std::optional
The custom optional implementation (from optional-bare, 2017) predates
C++17. All ESPHome platforms now compile with gnu++20, making
std::optional available everywhere.

The custom implementation had several issues:
- No emplace() support
- Always default-constructs value_ (wasteful for non-trivial types)
- reset() only flips a bool without destroying the value
- No move semantics
- Requires T to be default constructible

Replace with using aliases (using std::optional, using std::nullopt,
etc.) so all existing code using esphome::optional continues to work.

Also fix ~30 unsafe .value() calls across climate IR components that
relied on the custom optional's behavior of returning a
default-constructed value when empty. With std::optional, accessing
an empty optional is UB. These are replaced with value_or() using
appropriate defaults (CLIMATE_FAN_AUTO, CLIMATE_PRESET_NONE).
2026-02-27 16:09:19 -10:00

13 lines
170 B
C++

#pragma once
#include <optional>
namespace esphome {
using std::make_optional;
using std::nullopt;
using std::nullopt_t;
using std::optional;
} // namespace esphome