PlatformIO Community

Unable to use std::to_string function of C++11 with ESP32

I am trying to use std::to_string(), but get error: 'to_string' is not a member of 'std'. From this answer, it seems to be a problem with not supporting C++11. However, using “verbose build” as advised in this answer, I see -std=gnu++11 which I believe means that I am using C++11. It looks like I can use std::ostringstream instead, but I’d really like to understand why it is that I am unable to use std::to_string and whether I can change that. Here is the full output of my verbose build:

Processing nodemcu-32s (platform: espressif32; board: nodemcu-32s; framework: arduino; lib_deps: teemuatlut/TMCStepper@^0.7.3; 
monitor_speed: 115200)
-------------------------------------------------------------------------------------------------------------------------------CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/nodemcu-32s.html
PLATFORM: Espressif 32 (3.4.0) > NodeMCU-32S
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 3.10006.210326 (1.0.6)
 - tool-esptoolpy 1.30100.210531 (3.1.0)
 - toolchain-xtensa32 2.50200.97 (5.2.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 29 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <TMCStepper> 0.7.3 (C:\Users\jarat\Documents\PlatformIO\Projects\eqPlatformMain\.pio\libdeps\nodemcu-32s\TMCStepper)
|   |-- <SPI> 1.0 (C:\Users\jarat\.platformio\packages\framework-arduinoespressif32\libraries\SPI)
|-- <ESP32 BLE Arduino> 1.0.1 (C:\Users\jarat\.platformio\packages\framework-arduinoespressif32\libraries\BLE)
|-- <Preferences> 1.0 (C:\Users\jarat\.platformio\packages\framework-arduinoespressif32\libraries\Preferences)
Building in release mode
xtensa-esp32-elf-g++ -o .pio\build\nodemcu-32s\src\main.cpp.o -c -fno-rtti -fno-exceptions -std=gnu++11 -Os -g3 -Wall -nostdlib -Wpointer-arith -Wno-error=unused-but-set-variable -Wno-error=unused-variable -mlongcalls -ffunction-sections -fdata-sections 
-fstrict-volatile-bitfields -Wno-error=deprecated-declarations -Wno-error=unused-function -Wno-unused-parameter -Wno-sign-compare -fstack-protector -fexceptions -Werror=reorder -DPLATFORMIO=50203 -DARDUINO_NodeMCU_32S -DESP32 -DESP_PLATFORM -DF_CPU=240000000L -DHAVE_CONFIG_H -DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" -DARDUINO=10805 -DARDUINO_ARCH_ESP32 -DARDUINO_VARIANT=\"nodemcu-32s\" -DARDUINO_BOARD=\"NodeMCU-32S\" -Iinclude -Isrc -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\libraries\Preferences\src -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\libraries\BLE\src -I.pio\libdeps\nodemcu-32s\TMCStepper\src -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\libraries\SPI\src -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\config -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\app_trace -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\app_update -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\asio -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\bootloader_support -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\bt -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\coap -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\console -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\driver -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\efuse -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp-tls -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp32 -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp_adc_cal -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp_event -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp_http_client -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp_http_server -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp_https_ota -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp_https_server -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp_ringbuf -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp_websocket_client -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\espcoredump -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\ethernet -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\expat -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\fatfs -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\freemodbus -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\freertos -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\heap -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\idf_test -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\jsmn -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\json -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\libsodium -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\log -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\lwip -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\mbedtls -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\mdns -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\micro-ecc -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\mqtt -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\newlib -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\nghttp -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\nvs_flash -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\openssl -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\protobuf-c -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\protocomm -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\pthread -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\sdmmc -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\smartconfig_ack -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\soc -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\spi_flash -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\spiffs -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\tcp_transport -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\tcpip_adapter -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\ulp -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\unity -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\vfs -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\wear_levelling -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\wifi_provisioning -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\wpa_supplicant -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\xtensa-debug-module -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp-face -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\esp32-camera -IC:\Users\jarat\.platformio\packages\framework-src\main.cpp: In member function 'virtual void setup()::trackingCallback::onRead(BLECharacteristic*)':
arduinoespressif32\tools\sdk\include\esp-face -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\tools\sdk\include\fb_gfx -IC:\Users\jarat\.platformio\packasrc\main.cpp:270:33: error: 'to_string' is not a member of 'std'
ges\framework-arduinoesp       std::string trackingStr = std::to_string(tracking);
res                                 ^
sif32\cores\esp32 -IC:\Users\jarat\.platformio\packages\framework-arduinoespressif32\variants\nodemcu-32s src\main.cpp
*** [.pio\build\nodemcu-32s\src\main.cpp.o] Error 1

Thats a known bug (think its the compiler, but not sure).
My way:

template< class T, class = typename std::enable_if<
    std::is_integral<T>::value ||
    std::is_floating_point<T>::value>::type>
typename std::string NumericToString(T t)
{
    //  std::to_string is broken
    std::ostringstream stream;
if (sizeof(t) == 1)	//	otherwise interpreted as char and returned as-is
	stream << static_cast<int16_t>(t);
else
	stream << t;
    return stream.str();
}

(fyi: integral sadly includes also bool, so no error to see when calling with a bool)

Regards

This compiler version released by Espressif does not have std::to_string available. As outlined in https://github.com/platformio/platform-espressif32/issues/619#issuecomment-976525911, the new Arduino-ESP32 2.0 version uses a newer 8.4.0+2021r1 compiler version. When I use the platformio.ini

[env:esp32dev]
platform                    = https://github.com/platformio/platform-espressif32.git#feature/arduino-idf-master
platform_packages           = framework-arduinoespressif32 @https://github.com/espressif/arduino-esp32.git#2.0.1
                              platformio/tool-esptoolpy @https://github.com/tasmota/esptool/releases/download/v3.2/esptool-v3.2.zip
board = esp32dev
framework = arduino

and compile the code

#include <Arduino.h>
#include <iostream>
#include <string>

void setup() {
   Serial.begin(115200);
}

void loop() {
   std::string s = std::to_string(123);
   Serial.println(s.c_str());
}

I get

Processing esp32dev (platform: https://github.com/platformio/platform-espressif32.git#feature/arduino-idf-master; board: esp32dev; framework: arduino)
------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (3.3.1+sha.33ff413) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 0.0.0+sha.d218e58
 - tool-esptoolpy 1.30200.211025 (3.2.0)
 - toolchain-xtensa-esp32 8.4.0+2021r1
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 31 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\esp32dev\src\main.cpp.o
[..]
Linking .pio\build\esp32dev\firmware.elf
Retrieving maximum program size .pio\build\esp32dev\firmware.elf
Checking size .pio\build\esp32dev\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   7.1% (used 23340 bytes from 327680 bytes)
Flash: [====      ]  36.1% (used 472969 bytes from 1310720 bytes)
Building .pio\build\esp32dev\firmware.bin
esptool.py v3.2
Merged 2 ELF sections
============== [SUCCESS] Took 14.53 seconds ==============