4

So i'm making a program which requires std::variant, so obviously i need C++17 but that's not in Arduino without compiler flags, it's just I cant figure out what flags to do and how to change the compiler flags. (BTW i'm using windows, not Linux (though i do have a Linux laptop))

I have tried using the Arduino boost library but it seems to be written for C++ not Arduino even though it says it's made for Arduino, I have also tried workarounds but they use up too much memory.

I want it to have std or out least an std::variant equivalent in Arduino but I haven't found a way to get that.

Thanks in advance!

EDIT: If there isn't a way to do C++17 in Arduino, then can someone link a tutorial to code Arduino boards using C++

xtofl
  • 40,723
  • 12
  • 105
  • 192
PyPylia
  • 387
  • 3
  • 14

2 Answers2

0

The Arduino programming language is a subset of C++ with a few minor variations. You can reference .CPP and .H files in your project, as long as they don't use C++ features that aren't supported by Arduino.

I'm sure someone will eventually get creative and find a way to compile code with the C++17 features into AVR machine code (if they haven't already).

But keep in mind, these are tiny chips often running at 10-20 MHz with around 2 KB of working memory. The standard template library was a great leap forward for C++ but it might not be the best choice when every byte counts.

Doug Johnson
  • 558
  • 3
  • 9
  • I already know this, but when I tried to do `#include ` or whatever but it gave tons of errors due to it not having some standard libraries. I need some form of std::variant or boost::variant in Arduino or an Arduino library for C++17. – PyPylia May 12 '19 at 06:19
  • 1
    Try this: https://github.com/maniacbug/StandardCplusplus (btw: variant is standard for c++ 17, no need for boost anyway) However, I'm urging you to rethink design decisions - because stuff like: variant and any are not wise choice for AVR / MCU / low-level computing. I would go with pointers and reinterpret_cast - if you really need such functionality (but frankly: I wonder why you want it in Arduino?) - or, even better, with memcpy. – hardyVeles Feb 19 '20 at 21:37
  • 1
    I just stumbled over this, and must make a counterpoint to @hardyVeles std::variant is an *extremely* useful type for embedded work. It represents a sum type of the various sub-types with compact memory representation, simple value semantics that make programming errors more unlikely (other than pointers), and instead of unsafe reinterpret_cast allow for safe semantics with get_if. If you don't want to use the features of a language, fine. But broad statements about suitability (or lack thereof) need some actual supporting evidence. – deets Jun 08 '23 at 08:38
0

To use boost.variant check https://github.com/vancegroup/arduino-boost

iostream, sstream, string, variant and other header files like them are not available in avr-libc and in Arduino that uses avr-g++ (and avr-libc)

You can check https://github.com/maniacbug/StandardCplusplus AND https://github.com/mike-matera/ArduinoSTL

For allocating values without known datatypes you can write your class and view https://github.com/radinParsaei/Value

radinParsaei
  • 28
  • 1
  • 6