Boo0ns / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

Compilation error using "template <class T>" (prototypes generated incorrectly) #472

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I found an error using the last revision of Arduino IDE (0022).
This code doesn't compile correctly:

template <class T> int SRAM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
      EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int SRAM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
      *p++ = EEPROM.read(ee++);
    return i;
}

The IDE returns this error:
error: expected ',' or '...' before '&' token
error: ISO C++ forbids declaration of 'T' with no type
error: 'T' has not been declared

Using version 0021 it works fine.

Original issue reported on code.google.com by leomi...@gmail.com on 6 Feb 2011 at 3:31

GoogleCodeExporter commented 9 years ago
This is probably caused by the switch from the old oro.jar regular-expressions 
to the standard Java ones: 
<https://github.com/arduino/Arduino/commit/fa4d0582970e50c574ecfdcdbcfeae3754ca2
095>.  Properly generating function prototypes for the full range of C++ syntax 
is tricky, so you might be better off putting the templated function 
definitions in a separate header file in your sketch, which won't be 
pre-processed.  

Patches to the pre-processing code are welcome, too, although need to be well 
tested.

Original comment by dmel...@gmail.com on 21 Feb 2011 at 3:42

GoogleCodeExporter commented 9 years ago
Might it be helpful to put something in the programming documentation to this 
effect (that is, template functions are best pushed to separate header files 
that don't get pre-processed)?

I looked over the regex for the change, this would've bounced on the Perl5 
regex class in oro.jar, I'm pretty sure. Unless the submitter knows otherwise.

Original comment by WineBizR...@gmail.com on 9 Mar 2011 at 6:48

GoogleCodeExporter commented 9 years ago
This is still active in version 1.0.1 of the IDE. Simple example:

template <typename T> T myAbs (const T a)
  {
  return (a < 0) ? - a : a;
  } // end of myAbs

void setup () {}
void loop () {}

Gives error:

sketch_jun21a:-1: error: 'T' does not name a type

It can't be too hard to fix the regexp to at least handle that, and similar, 
can it? eg.

Optional: template < ... > preceding the function declaration.

Original comment by n...@gammon.com.au on 20 Jun 2012 at 9:06

GoogleCodeExporter commented 9 years ago

Original comment by dmel...@gmail.com on 31 Jul 2012 at 12:48

GoogleCodeExporter commented 9 years ago
New preprocessor tracked at https://github.com/arduino/Arduino/pull/2636. 
Builds for testing it are available

Original comment by federico...@gmail.com on 13 Feb 2015 at 7:18