2

Supposedly Arduino's IDE > 1.6.2 has C++11 support.

I have just freshly downloaded and run version 1.6.9 on OSX (and as others have reported, this repros on Windows as well, with 1.6.9/1.6.10).

I cannot get this simple program to compile:

constexpr int get_five() { return 5; }

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

void loop() {
}

I receive this error when I try to build or upload:

sketch_jul25a:1: error: 'constexprint' does not name a type
 constexpr int get_five() { return 5; }
 ^
exit status 1
'constexprint' does not name a type

I've looked at this question and answer, but it is supposedly no longer applicable in 1.6.9 version of the IDE that I am using - error: 'constexpr' does not name a type m- arduino ide

I have dug into the temporary files that are output by the IDE when building, and it seems it is trying to automatically generate headers for functions (I assume for multi-file sketch support), and does the wrong thing when it encounters constexpr:

#include <Arduino.h>
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexprint get_five(); // **** <- This looks to be the culprit
#line 3 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void setup();
#line 9 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void loop();
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexpr int get_five() { return 5; }

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

void loop() {
}

Is this a bug in the Arduino IDE? Is it unique to OSX? Is there a workaround that allows constexpr to work?

In googling I have found that some Arduino libraries are using constexpr, so I am assuming it could be made to work in some cases.

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • I experienced the same issue. This issue isn't OSX specific. This issue also occurs in 1.6.10. – esorton Aug 01 '16 at 18:34
  • Naming the error in the title is better, so I approved the edit. Other people might not have quite the same error, for example if they're using a `uint`, etc. Hopefully this plus the body is still enough to help find it. This is definitely an error in an upstream component from the IDE itself (the component is arduino-builder), as the accept answer states. They tend to release a whole new slightly updated stack with each IDE update, though. – Merlyn Morgan-Graham Aug 01 '16 at 19:19

1 Answers1

3

This is a known limitation of the arduino-builder.

Until it is fixed, you can add a prototype yourself above the function. This will prevent the IDE from incorrectly generating its own.

constexpr int get_five();
constexpr int get_five() { return 5; }

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

void loop() {
}
Chris A
  • 1,475
  • 3
  • 18
  • 22
  • Your work around solves my issue, and I will accept when the 5 mins is up. Do you have a link to the known issue? Google hasn't helped me so far :) – Merlyn Morgan-Graham Jul 25 '16 at 21:34
  • I found this issue that it looks like you may have commented on - https://github.com/arduino/arduino-builder/issues/30 - though it looks like it is closed. – Merlyn Morgan-Graham Jul 25 '16 at 21:38
  • 2
    I thought there was one specifically for `constexpr`. I have created one now: https://github.com/arduino/arduino-builder/issues/170 – Chris A Jul 25 '16 at 21:41