Static template function in Arduino IDE 2.3.3

I tried to compile the following code [added to the Blink-example]:

template<size_t byteLength>
static int memvcmp(uint8_t const * memory, uint8_t const value)
{
  if (0 == byteLength)
  {
    return 0;
  }
  else
  {
    int const firstByteComparison = memcmp(memory, &value, 1);
    return (0 == firstByteComparison) ? memcmp(memory, memory + 1, byteLength - 1) : firstByteComparison;
  }
}

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
  delay(1000); // wait for a second
  digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
  delay(1000); // wait for a second
}

It fails with the following error message:

C:\Users...\AppData\Local\Temp.arduinoIDE-unsaved2024103-15436-pctuf.wrhpp\Blink\Blink.ino: In function 'int memvcmp(const uint8_t*, uint8_t)':
C:\Users...\AppData\Local\Temp.arduinoIDE-unsaved2024103-15436-pctuf.wrhpp\Blink\Blink.ino:27:14: error: 'byteLength' was not declared in this scope
if (0 == byteLength)
^~~~~~~~~~

exit status 1

Looking into the temporarily generated file it is obvious, that the template-declaration is missing from the implementation:

template<size_t byteLength>
#line 25 "C:\\Users\\...\\AppData\\Local\\Temp\\.arduinoIDE-unsaved2024103-15436-pctuf.wrhpp\\Blink\\Blink.ino"
static int memvcmp(uint8_t const * memory, uint8_t const value);
#line 40 "C:\\Users\\...\\AppData\\Local\\Temp\\.arduinoIDE-unsaved2024103-15436-pctuf.wrhpp\\Blink\\Blink.ino"
void setup();
#line 46 "C:\\Users\\...\\AppData\\Local\\Temp\\.arduinoIDE-unsaved2024103-15436-pctuf.wrhpp\\Blink\\Blink.ino"
void loop();
#line 25 "C:\\Users\\...\\AppData\\Local\\Temp\\.arduinoIDE-unsaved2024103-15436-pctuf.wrhpp\\Blink\\Blink.ino"
static int memvcmp(uint8_t const * memory, uint8_t const value)
{
  if (0 == byteLength)
  {
    return 0;
  }
  else
  {
    int const firstByteComparison = memcmp(memory, &value, 1);
    return (0 == firstByteComparison) ? memcmp(memory, memory + 1, byteLength - 1) : firstByteComparison;
  }
}

Removing the static keyword from the template declaration solves this problem.

However, as the C++-standard supports static templates, I think the Arduino IDE should not prohibit its use.

Did I do something wrong? Can anyone else confirm this observation? Whom do I have to reach in order for this to be fixed?

Thanks for any feedback

Johannes

Please post your full sketch

1 Like

I don't know much about this stuff. I know that there are C++ standards but static templates might not always have been in the C++ standard.

Further the C++ compiler that is used by the board package needs to support the version of the C++ standard that has static templates specified. E.g. the avr-gcc used by Arduino is an old version so does not support the latest C++ standard.

I do not know as I only have a very basic understanding of C++ and templates are far away for me.

The usual approach to errors caused by the builder is to use .h and .cpp files and put problematic code in there; it's anyway a good approach. The builder will only process .ino files and hence not touch .h and .cpp files so that might solve the issue (assuming the compiler supports the use fo static templates).

Probably GitHub - arduino/arduino-cli: Arduino command line tool. However don't hold your breath that it will be fixed soon.

The first code block is the full code. Nothing more required. You might however have to scroll in the code block...

The compiler is an avr-gcc 7.3.0:

"C:\Users\...\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++"

I used it to compile the original ino-file myself [ino -> o - without linker]. This worked fine.

The problem here is that the function definition in the auto-generated temporary file is missing the template<size_t byteLength>-part.

#line 25 "C:\\Users\\...\\AppData\\Local\\Temp\\.arduinoIDE-unsaved2024103-15436-pctuf.wrhpp\\Blink\\Blink.ino"
static int memvcmp(uint8_t const * memory, uint8_t const value)

Thanks for that.

I understand that. In which case you should not have mentioned the C++ standard as it does not sound relevant :wink:

There are enough known issues with the build process. Generated function prototype injected before declaration of custom parameter type · Issue #2696 · arduino/arduino-cli · GitHub is one of them and contains a list of related reports. You can start a new issue in the link that I gave if your scenario is not covered.

The automatic protype generator has problems with templates.
It works if it is on the same line:

template<size_t byteLength> static int memvcmp(uint8_t const * memory, uint8_t const value)
{
...
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.