Are smart pointers full implemented in Mbed OS?

Does anyone know if there are any limitations with using Smart Pointers in C++ in Mbed OS?

I happened to try the following code snippet:

    std::shared_ptr<int[]> arr = std::shared_ptr<int[]>(new int[20]);
    arr[0] = 0;

This won’t compile in Mbed Studio, but does in Linux (g++ and Clang, C++ 11 onwards).

which compiler are you using in Mbed Studio?

See my answer here: Embedded, Mbed OS & Smart pointers - #5 by ladislas

The default - ARM C6

Hi @noutram,

Any chance you could provide:

  • Any specific output/errors from the toolchain
  • The compile (command line) options being passed to AC6


Carey - Studio Team

[Error] main.cpp@7,34: no matching conversion for functional-style cast from 'int *' to 'std::shared_ptr<int []>' [Error] main.cpp@8,8: type 'std::shared_ptr<int []>' does not provide a subscript operator [ERROR] .\main.cpp:7:34: error: no matching conversion for functional-style cast from 'int *' to 'std::shared_ptr<int []>' std::shared_ptr<int[]> arr = std::shared_ptr<int[]>(new int[20]);

In the mbed library, Tools/profiles/debug.json, I can see the following:

"ARMC6": { "common": ["-c", "--target=arm-arm-none-eabi", "-mthumb", "-g", "-O1", "-Wno-armcc-pragma-push-pop", "-Wno-armcc-pragma-anon-unions", "-Wno-reserved-user-defined-literal", "-Wno-deprecated-register", "-DMULADDC_CANNOT_USE_R7", "-fdata-sections", "-fno-exceptions", "-MMD", "-fshort-enums", "-fshort-wchar", "-DMBED_DEBUG", "-DMBED_TRAP_ERRORS_ENABLED=1"], "asm": [], "c": ["-D__ASSERT_MSG", "-std=gnu11"], "cxx": ["-fno-rtti", "-fno-c++-static-destructors", "-std=gnu++14"], "ld": ["--verbose", "--remove", "--show_full_path", "--legacyalign", "--any_contingency", "--keep=os_cb_sections"] },

Thank you @noutram.

Sorry for the late reply, I had to seek some clarification with another team. shared_ptr to array isn’t supported until C++17, which Arm Compiler 6 only provides as a COMMUNITY feature currently: Documentation – Arm Developer

But passing std=c++17 should allow your code above to compile.

I’m not sure why GCC appears to be backwards compatible with this.


Carey - Studio Team


Thanks - that is really useful to know. In fact, having read the link you provide, I am inclined to add
-Wpedantic to make it clearer to students. In this context, risk management is more important than new language features for sure!



1 Like

I’m curious what you mean about “risk management” in this context :slight_smile:

Good question. Maybe a better expression is “defensive coding”.
If developing software to run unsupervised 24/7, maybe sticking to the language version Arm recommend rather than leaping for newer and greater. Presumably mbed os has a QA process, and this is done using c++14.