0

I'm trying to write a generic template for Print for an Arduino project. In doing so, I encountered a compiler error I didn't understand, so I made a minimum reproducible example.

template <typename T, typename... T2>
void print_all(Print* const (&ps)[2], T v1, T2... v2) {
    ps[0]->print(v1);
    ps[0]->flush();
    ps[1]->print(v1);
    ps[1]->flush();
    print_all(ps, v2...);
}
void print_all(Print* const (&ps)[2]) {
    return;
}

void setup() {
    print_all({&Serial1, &Serial2}, 1.0, "abc", 5ul);
}

void loop() {
}

The error I get is error: expected ')' before ';' token with no line number information. If I insert an empty line at the start of the file, the compiler error changes to

error: expected ')' before ';' token
    1 | 
      |                                                                       ^
      |                                                                       )

I tried this with avr-gcc version 7.3.0 (official arduino avr board) and 14.1.0 (using arch linux arduino avr boards) and get the same error either way.

I experimented for a while, and eventually put the code on Compiler Explorer using avr-gcc instead of Arduino IDE to compile. I had to provide some stubs to make everything compile, but it seems to work as I would expect. So why does my code work on compiler explorer, but not in the Arduino IDE?

code with supporting method stubs

class Print{
    public:
    virtual void print(unsigned long i) {
    }
    virtual void print(double i) {
    }
    virtual void print(const char* i) {
    }
    virtual void flush() {

    }
};

class Serial: public Print {};

Serial serial1 = Serial{};
Serial serial2 = Serial{};

template <typename T, typename... T2>
void print_all(Print* const (&ps)[2], T v1, T2... v2) {
    ps[0]->print(v1);
    ps[0]->flush();
    ps[1]->print(v1);
    ps[1]->flush();
    print_all(ps, v2...);
}
void print_all(Print* const (&ps)[2]) {
    return;
}

int main() {
    Print* const ps[] = {&serial1, &serial2};
    print_all(ps, 1.0, "abc", 5ul);
}
5
  • Did you turn on C++11 or higher on the arduino? Is that really the full error message? Commented Sep 14, 2024 at 1:04
  • I don't know how to turn or off C++11 in the arduino IDE, but verbose compiler output shows all files are compiled with -std=gnu++11 so its on. I omitted the part of the error message that has the path to the file in which the error occurred, but there's no new or interesting information there, just a repetition of the same cryptic error message Commented Sep 14, 2024 at 1:51
  • Change it to -std=c++11 and refer to What are the differences between -std=c++11 and -std=gnu++11? Commented Sep 14, 2024 at 3:05
  • changing to -std=c++11 doesn't fix the errors, nor can I find a difference outlined in the linked question that applies to my situation Commented Sep 14, 2024 at 4:23
  • Arduno IDE uses some kind of quirky non-standard compiler to make it easier for beginners to produce working code fast, but that same quirkiness interferes with more advanced uses of the language. Commented Sep 14, 2024 at 6:32

2 Answers 2

1

From the docs:

First, the Arduino development software performs some minor pre-processing to turn your sketch into a C++ program

So your Arduino code is not really a C++ program. It is an input for the Arduino preprocessor which outputs a C++ program.

What does the preprocessor do? Among other things:

Prototypes are generated for all function definitions in .ino/.pde files that don't already have prototypes. In some rare cases, prototype generation may fail for some functions. To work around this, you can provide your own prototypes for these functions

Well, congratulations I guess? you have stumbled upon one of those "rare" cases. Unfortunately, providing a separate template doesn't help. However there is another workaround: place the template header on one line. Yes, you've read it right.

// this works
template <typename T, typename... T2> void print_all(Print* const (&ps)[2], T v1, T2... v2) {

// this doesn't
template <typename T, typename... T2>
void print_all(Print* const (&ps)[2], T v1, T2... v2) {
Sign up to request clarification or add additional context in comments.

2 Comments

You found the exact problem and how to fix it, thanks. I read through a little more of the documentation you linked and it states that "No pre-processing is done to files in a sketch with any extension other than .ino or .pde". I tested this by making a file test.h in the same folder as my example sketch and did #include "test.h" from the .ino file. Doing that, the template works as expected, so thats both another workaround and confirmation of the problem. Do you know though - is there a way to see the output of the arduino pre-processing?
I found it by using Preferences > show verbose output during compilation in the IDE. I then found a line near the top of the output when "verifying" that started with arduino-builder -compile and had a flag in it -build-path /tmp/arduino_build_598009 I went to /tmp/arduino_build_598009/sketch/sketch_sep13a.ino.cpp which contains the pre-processed output. I copy pasting the pre processed output into compiler explorer, which now reproduces the error message generated by the Arduino IDE!
0

The answer posted by user n. m. could be an AI is correct, but there's a solution that seems less fragile than his. From the docs on the Arduino build process

Prototypes are generated for all function definitions in .ino/.pde files that don't already have prototypes.

So a simple robust solution is to place items for which prototype generation failed in another file in the sketch folder with an extension other than .ino/.pde such as util.cpp, then #include util.cpp

Sketch.ino

#include "util.cpp"

void setup() {
    print_all({&Serial1, &Serial2}, 1.0, "abc", 5ul);
}

void loop() {
}

util.cpp

void print_all(Print* const (&ps)[2]) {
    (void)ps; //no unused warning
    return;
}

//this works, even though it wouldn't if it were located in sketch.ino
template <typename T, typename... T2>
void print_all(Print* const (&ps)[2], T v1, T2... v2) {
    ps[0]->print(v1);
    ps[0]->flush();
    ps[1]->print(v1);
    ps[1]->flush();
    print_all(ps, v2...);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.