Skip to content

Cannot compile a function template after a struct definition #2977

@Ebola-Chan-bot

Description

@Ebola-Chan-bot

Describe the problem

Unable to compile a function template after a struct definition. The code passed compilation with multiple different versions of the compilers on Compiler Explorer, but fails in Arduino IDE.

To reproduce

Setup environment

$ arduino-cli version

arduino-cli  Version: git-snapshot Commit: eb4e2ca77 Date: 2025-08-14T05:11:39Z

$ mkdir "/tmp/FooSketch"

$ echo '
struct foo_t{};
template<typename T>
void bar(foo_t Parameter) {}
void setup() {}
void loop() {}
' > "/tmp/FooSketch/FooSketch.ino"

Demo

$ arduino-cli compile --fqbn arduino:avr:uno "/tmp/FooSketch"

C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:2:30: error: variable or field 'bar' declared void
 struct foo_t{};
                              ^
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:2:30: error: 'foo_t' was not declared in this scope
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:2:30: note: suggested alternative: 'fpos_t'
 struct foo_t{};
                              ^
                              fpos_t

Used platform Version Path
arduino:avr   1.8.6   C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Error during build: exit status 1

🐛 There was a spurious compilation failure of valid code.

By looking at the C++ code generated by the Arduino sketch preprocessor, we can see the cause of the error:

$ arduino-cli compile --fqbn arduino:avr:uno --preprocess "/tmp/FooSketch"

#include <Arduino.h>
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"

#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
template<typename T>void bar(foo_t Parameter);
#line 5 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void setup();
#line 6 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void loop();
#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
struct foo_t{};
template<typename T>
void bar(foo_t Parameter) {}
void setup() {}
void loop() {}

🐛 The spurious compilation failure was caused by Arduino CLI placing the prototype for the bar function before the declaration of the foo_t type.

Expected behavior

Successful compilation

Arduino CLI version

eb4e2ca

Operating system

Windows

Operating system version

11

Additional context

Related

Workaround

Manually add a function prototype at the appropriate location:

struct Struct{};
template<typename T>
void Function(Struct Parameter);  // Manually added function prototype to work around prototype generator bug.
template<typename T>
void Function(Struct Parameter) {}
void setup() {}
void loop() {}

Issue checklist

  • I searched for previous reports in the issue tracker
  • I verified the problem still occurs when using the latest nightly build
  • My report contains all necessary details

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: build-processRelated to the sketch build processtopic: codeRelated to content of the project itselftype: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions