Experiences with the STL and C++11 on the Photon

One of the things I was excited about with the Photon (as opposed to the Arduino family of microcontrollers) is the out-of-the-box support for C++11 and the STL in the cloud compiler. The fact that you can do this without having to install any libraries or change any settings in the IDE seems pretty cool:

#include <vector>

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

void loop() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    
    for (auto i : v) {
        Serial.println(i);
    }

    Serial.println();
}

Which of course prints:

1
2
3
4
5

I’m curious if anyone has any experiences to share developing with “modern” C++ on the Core/Photon. Do you use the STL, range-based-for-loops, auto, lambdas, etc.? Or are these features not as wonderful in embedded work as they are when developing for regular computers?

1 Like

By all means, take advantage of the expressiveness of C++11 to write compact yet clear code! The STM32 microcontroller in the Photon has enough oomph to handle running that code (i.e. don’t worry at all about performance vs pure C).

You’re ahead of the curve if you already understand all that new syntax!

4 Likes

We use the C++11 syntax heavily in system firmware, so no reason not to also use it in your application code!

I learnt C++ before the standardization process and really welcome all the new features - it’s much more pleasant to code in than it used to be!

3 Likes

@mdma, can you tell me how to get access to the STL libraries if I download the photon firmware github to compile using Eclipse? I’m bringing over a larger project from the Teensy, and the WebIDE isn’t able to handle it very well. This also allows me to use git for the project to track the changes. However, STL doesn’t seem to be part of the firmware image you can download from github in the spark/photon area.

Any help would be appreciated. Thanks.

@mdma, sorry, Eclipse wasn’t picking up the compiler std library for some reason. I think I was able to fix it, but am still having issues getting builds to work with Eclipse and Makefiles.