Hi!
C++17 rocks! However, the different Arduino IDE's are still using C++11. I tweaked the compilation instruction so it was able to compile using C++17, and gracefully it compiled, so good for being true
For testing purposes I've compiled this C++17 code:
// (1) Structured binding
// (2) If initializer
struct Res{int x; int y;};
auto foo( int a ) -> Res
{
return{ a+1, a*2 }; // (1)
}
void setup()
{
Serial.begin( 9600 );
}
void loop()
{
auto [a,b] = foo( 5 ); // (1)
Serial.println( a );
Serial.println( b );
if( auto [x,y] = foo( 3 ); x < 10 ) // (2)
{
Serial.println( y );
}
}
An excerpt from the compilation output (just to show it's actually using C++17 and that it compiled):
home/fjrg76/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++17 ...
Generating function prototypes...
...
Compiling sketch...
/home/fjrg76/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -Wall -Wextra -std=gnu++17 ...
Compiling libraries...
Compiling core...
Using precompiled core: /tmp/arduino_cache_911616/core/core_arduino_avr_uno_6da0409820faaaedb6b0232ec4092644.a
Linking everything together...
...
Sketch uses 1736 bytes (5%) of program storage space. Maximum is 32256 bytes.
Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.
C++ compiler version:
$ ./avr-g++ --version
avr-g++ (GCC) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$
Is there any issue with this compiler for the ATMEGA328 processor (and alike) that has compelled the Arduino's mainteiners not to use it?
Same thing for Cortex ARM cores: even the Due board is still using C++11. Any reason?
Compiler version for ARM Cortex cores:
$ ./arm-none-eabi-g++ --version
arm-none-eabi-g++ (GNU Arm Embedded Toolchain 10.3-2021.10) 10.3.1 20210824 (release)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$
It's been 6 years from C++17 and right now people is working in the C++23 release, so I think that C++17 is safe enough to be used in embedded systems.
Greetings!