How can I utilize the full C++ standard library in the Arduino IDE if you have performance microcontroller?

I am currently using Arduino IDE 2.3.2 to write C++ code. Although this does allow me to compile code using C++ standard library features like std::unique_ptr(and of course supports the base language features like uint8_t), it is missing support for significant portions of the standard library, such as std::vector, std::string, iterators, etc.

Memory is a very common problem, but the ESP32-S3 can even run Linux (Reddit - Dive into anything; Linux 5.0 запустили на процессоре ESP32 с 8 МБ RAM / Хабр)so it's unlikely to be a problem for it.

What do I need to do in order to be able to access the complete C++ standard library when using the Arduino IDE to target Arduino boards? Do I need to upgrade my IDE to a later version? Do I need to install a different toolchain? Do I need to switch to a different Arduino board that provides more capabilities?

I’m unsure !! But this is what I think !

The commands/instructions are what you get. Unless you want to get into developing the IDE ( which I guess you can do ?) that’s your lot .

But at the end of the day these are all standard processors widely used in industry . I’m sure all of industry doesn’t use the Arduino ide
There is Atmel Studio for the Atmel processors , Visual studio and so on .
Visual studio , I would think worth a look ( it is free)
You’ll probably find limitations with libraries , or using them .
Have a look at chip manufacturers specific development systems .

Arduino has an advantage that it supports many devices from different manufacturers and has extensive library and support .
You might find it easier to forget using those C commands of which you speak - there is always a work around . There may also be good reason ( maybe even historical( eg dynamic string length. - smacks of problems where memory is limited ) why they are not included .)
You might be able to write a library so you can use this stuff , but life is too short .
Btw 512kb of ram is tiny compared to typical desktops .

Example

1 Like

You can't, at least not supplied by Arduino. There are parts implemented by 3rd parties in various libraries though. The reason is anything that is memory hungry is not a good fit for Arduino. Even Strings are a problem, using them too much will usually end in a crash.
I find it amusing that us old timers who only know C are not impacted by this, maybe we need to add good old K&R C to the modern curiculum.

2 Likes

quick port of a simple vector test program to ESP32

#include <vector>
#include <string>
#include <iostream>

using namespace std;

void printVector(vector<string> vec) {
  // change characters in strin to upercase
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    for (int i = 0; i < (*it).size(); i++)
      (*it)[i] = toupper((*it)[i]);
    cout << *it << endl;  // print result
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("\nvector of strings ");
  delay(1000);
  vector<string> vec; // create vector of strings
  vec.push_back("hello");
  vec.push_back("£sam");
  vec.push_back("bye");
  printVector(vec);
}

void loop() {}

serial monitor displays

vector of strings 
HELLO
£SAM
BYE
1 Like

it is not

1 Like

Me too, actually more than pretty sure.
I'm absolutely certain of it, since I've used them dozens of times :sweat_smile:

1 Like

@horace Yes it is working!!! Then where did the information come from that they (vector, string, exception) do not exist, and that if they exist, they will be slow and will not work correctly? Or does the ESP32-S3 only have all this?

It depends, it changes depending on each framework and the capabilities of the MCU.

With ESP32, in addition to all the STL C++ containers, for example you could also use

try {
}
catch {
}

statements if you need it.

1 Like

just ran the following on a Raspberry Pi Pico RP2040

#include <vector>
#include <string>
//#include <iostream>

using namespace std;

void printVector(vector<string> vec) {
  // change characters in strin to upercase
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    for (int i = 0; i < (*it).size(); i++)
      (*it)[i] = toupper((*it)[i]);
    //cout << *it << endl;  // print result
    Serial.println((*it).c_str());
  }
}


void setup() {
  Serial.begin(115200);
  while (!Serial)
    ;
  delay(1000);
  Serial.println();
  Serial.println("\nvector of strings ");
  delay(2000);
  vector<string> vec;  // create vector of strings
  vec.push_back("hello");
  vec.push_back("£sam");
  vec.push_back("bye");
  printVector(vec);
}

void loop() {}

note iostream did not work

serial monitor output

vector of strings 
HELLO
£SAM
BYE
1 Like

You tell us.

The STL is also available for ARM-based boards.

1 Like

So speed and efficient will remain with vector if we will compare to usual array like in usual C++ where it remain?

even though it is possible to run simple STL test programs on ESP32, RPI Pico etc they are still microcontrollers with limited RAM, i.e. NOT not PCs or even Raspberry Pi 4 with Gbytes of RAM and virtual memory operating systems
using STL vector, list, queue, etc can fragment the memory and cause problems
I would still tend to use C type arrays

2 Likes

Not about AMDuinos but with AVRduinos you only want to use some parts of C++ and avoid dynamic allocation and all Container Classes like the plague.

On small environment hardware, what fits well is C. Nice, tight next to the metal C. Char array strings C. Only ASM fits closer C.

2 Likes

I asked it because I have seen a lot of this information on the internet:)

Meh, YMMV. I've seen large ESP32 applications that use the STL. And many of the sophisticated ESP32 libraries use it.

@horace @gfvalvo Can you explain what is fragmentation memory and what the difference between memory leak? And what about effecient?

That's a pretty nebulous question. The STL is written by highly-experienced programmers, is thoroughly-tested, and is nearly bulletproof when used per its documentation. I'm guessing you'd be hard pressed to implement something more "efficient" without going to assembly language ... which would not be nearly as flexible. What is your real concern?

@gfvalvo Thank you, I will explain, what I asked: I know that STL is very efficient library, but how good it was implemented in MCU, I do not know

only 8-bit AVR Arduinos don't have the standard library

2 Likes

AVRduinos have the C Standard Library via AVR LibC.
Not C++ STL but can be used by Gnu C++.

Is C++ not written in C?

It has moved to GitHub but historic docs are here.