From 5637116378d5a01c7783f0f90bf1beade4341547 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 19 Mar 2026 08:43:22 -1000 Subject: [PATCH] [mqtt] Replace std::bind with lambdas in CustomMQTTDevice (#14964) --- esphome/components/mqtt/custom_mqtt_device.h | 25 ++++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/esphome/components/mqtt/custom_mqtt_device.h b/esphome/components/mqtt/custom_mqtt_device.h index 09ed7bd6d1..a003379fe0 100644 --- a/esphome/components/mqtt/custom_mqtt_device.h +++ b/esphome/components/mqtt/custom_mqtt_device.h @@ -189,28 +189,33 @@ class CustomMQTTDevice { template void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(const std::string &, const std::string &), uint8_t qos) { - auto f = std::bind(callback, (T *) this, std::placeholders::_1, std::placeholders::_2); - global_mqtt_client->subscribe(topic, f, qos); + auto *obj = static_cast(this); + global_mqtt_client->subscribe( + topic, [obj, callback](const std::string &t, const std::string &payload) { (obj->*callback)(t, payload); }, qos); } template void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(const std::string &), uint8_t qos) { - auto f = std::bind(callback, (T *) this, std::placeholders::_2); - global_mqtt_client->subscribe(topic, f, qos); + auto *obj = static_cast(this); + global_mqtt_client->subscribe( + topic, [obj, callback](const std::string &, const std::string &payload) { (obj->*callback)(payload); }, qos); } template void CustomMQTTDevice::subscribe(const std::string &topic, void (T::*callback)(), uint8_t qos) { - auto f = std::bind(callback, (T *) this); - global_mqtt_client->subscribe(topic, f, qos); + auto *obj = static_cast(this); + global_mqtt_client->subscribe( + topic, [obj, callback](const std::string &, const std::string &) { (obj->*callback)(); }, qos); } template void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(const std::string &, JsonObject), uint8_t qos) { - auto f = std::bind(callback, (T *) this, std::placeholders::_1, std::placeholders::_2); - global_mqtt_client->subscribe_json(topic, f, qos); + auto *obj = static_cast(this); + global_mqtt_client->subscribe_json( + topic, [obj, callback](const std::string &t, JsonObject root) { (obj->*callback)(t, root); }, qos); } template void CustomMQTTDevice::subscribe_json(const std::string &topic, void (T::*callback)(JsonObject), uint8_t qos) { - auto f = std::bind(callback, (T *) this, std::placeholders::_2); - global_mqtt_client->subscribe_json(topic, f, qos); + auto *obj = static_cast(this); + global_mqtt_client->subscribe_json( + topic, [obj, callback](const std::string &, JsonObject root) { (obj->*callback)(root); }, qos); } } // namespace esphome::mqtt