1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-31 07:03:55 +00:00

we have c++20

This commit is contained in:
J. Nick Koston
2025-10-26 12:01:54 -07:00
parent b68d030f5a
commit 48b45ba439

View File

@@ -27,20 +27,21 @@ template<typename T, typename... X> class TemplatableValue {
public:
TemplatableValue() : type_(NONE) {}
template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0> TemplatableValue(F value) : type_(VALUE) {
template<typename F>
requires(!std::invocable<F, X...>) TemplatableValue(F value) : type_(VALUE) {
new (&this->value_) T(std::move(value));
}
// For stateless lambdas (convertible to function pointer): use function pointer
template<typename F, enable_if_t<is_invocable<F, X...>::value && std::is_convertible<F, T (*)(X...)>::value, int> = 0>
TemplatableValue(F f) : type_(STATELESS_LAMBDA) {
template<typename F>
requires std::invocable<F, X...> && std::convertible_to<F, T (*)(X...)> TemplatableValue(F f)
: type_(STATELESS_LAMBDA) {
this->stateless_f_ = f; // Implicit conversion to function pointer
}
// For stateful lambdas (not convertible to function pointer): use std::function
template<typename F,
enable_if_t<is_invocable<F, X...>::value && !std::is_convertible<F, T (*)(X...)>::value, int> = 0>
TemplatableValue(F f) : type_(LAMBDA) {
template<typename F>
requires std::invocable<F, X...> &&(!std::convertible_to<F, T (*)(X...)>) TemplatableValue(F f) : type_(LAMBDA) {
this->f_ = new std::function<T(X...)>(std::move(f));
}