Template with parameter pack compiles using avr-gcc, but not using Arduino IDE

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

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) {

2

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...);
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Template with parameter pack compiles using avr-gcc, but not using Arduino IDE

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

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) {

2

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...);
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật