diff --git a/esphome/components/wifi/wifi_component_esp8266.cpp b/esphome/components/wifi/wifi_component_esp8266.cpp index 59909b2cb5..4e17c42f41 100644 --- a/esphome/components/wifi/wifi_component_esp8266.cpp +++ b/esphome/components/wifi/wifi_component_esp8266.cpp @@ -706,10 +706,10 @@ void WiFiComponent::wifi_scan_done_callback_(void *arg, STATUS status) { this->scan_result_.init(count); for (bss_info *it = head; it != nullptr; it = STAILQ_NEXT(it, next)) { - WiFiScanResult res({it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]}, - std::string(reinterpret_cast(it->ssid), it->ssid_len), it->channel, it->rssi, - it->authmode != AUTH_OPEN, it->is_hidden != 0); - this->scan_result_.push_back(res); + this->scan_result_.emplace_back( + bssid_t{it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]}, + std::string(reinterpret_cast(it->ssid), it->ssid_len), it->channel, it->rssi, it->authmode != AUTH_OPEN, + it->is_hidden != 0); } this->scan_done_ = true; } diff --git a/esphome/components/wifi/wifi_component_esp_idf.cpp b/esphome/components/wifi/wifi_component_esp_idf.cpp index 951f5803a6..a483e893e9 100644 --- a/esphome/components/wifi/wifi_component_esp_idf.cpp +++ b/esphome/components/wifi/wifi_component_esp_idf.cpp @@ -790,8 +790,8 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) { bssid_t bssid; std::copy(record.bssid, record.bssid + 6, bssid.begin()); std::string ssid(reinterpret_cast(record.ssid)); - WiFiScanResult result(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, ssid.empty()); - scan_result_.push_back(result); + scan_result_.emplace_back(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, + ssid.empty()); } } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) { diff --git a/esphome/components/wifi/wifi_component_libretiny.cpp b/esphome/components/wifi/wifi_component_libretiny.cpp index cb179d9022..45e2fba82a 100644 --- a/esphome/components/wifi/wifi_component_libretiny.cpp +++ b/esphome/components/wifi/wifi_component_libretiny.cpp @@ -419,9 +419,9 @@ void WiFiComponent::wifi_scan_done_callback_() { uint8_t *bssid = WiFi.BSSID(i); int32_t channel = WiFi.channel(i); - WiFiScanResult scan({bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]}, std::string(ssid.c_str()), - channel, rssi, authmode != WIFI_AUTH_OPEN, ssid.length() == 0); - this->scan_result_.push_back(scan); + this->scan_result_.emplace_back(bssid_t{bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]}, + std::string(ssid.c_str()), channel, rssi, authmode != WIFI_AUTH_OPEN, + ssid.length() == 0); } WiFi.scanDelete(); this->scan_done_ = true; diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 326718e974..37a64d46b2 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -281,13 +281,13 @@ template class FixedVector { } } - /// Emplace element without bounds checking - constructs in-place + /// Emplace element without bounds checking - constructs in-place with arguments /// Caller must ensure sufficient capacity was allocated via init() /// Returns reference to the newly constructed element /// NOTE: Caller MUST ensure size_ < capacity_ before calling - T &emplace_back() { - // Use placement new to default-construct the object in pre-allocated memory - new (&data_[size_]) T(); + template T &emplace_back(Args &&...args) { + // Use placement new to construct the object in pre-allocated memory + new (&data_[size_]) T(std::forward(args)...); size_++; return data_[size_ - 1]; }