Is this a compiler bug? typedef'd function pointer used as parameter type fails

Hi,

I am using the arduino IDE version 2:1.0.5+dfsg-4

The following compiles ok:

typedef void (*funcptr)();

void myfunc(void (*f)()) {
  
}

void setup() {
  
}

void loop() {
  
}

The following does not:

typedef void (*funcptr)();

void myfunc(funcptr f) {
  
}

void setup() {
  
}

void loop() {
  
}

It reports the error:

Silly002.ino:2:13: error: variable or field ‘myfunc’ declared void
Silly002.ino:2:13: error: ‘funcptr’ was not declared in this scope

The IDE creates function prototypes for you. Unfortunately, it puts them up above where your typedef is. Write your own prototype and that won't happen.

I'll answer this myself. Digging around the documentation on the build process, we find:

"Next, the environment searches for function definitions within your main sketch file and creates declarations (prototypes) for them. These are inserted after any comments or pre-processor statements (#includes or #defines), but before any other statements (including type declarations). This means that if you want to use a custom type as a function argument, you should declare it within a separate header file."

As someone familiar with C++, but new to the Arduino platform, I was unaware the IDE was doing this. (Being familiar with C and C++, I was creating prototypes for functions manually when they were required.) It would be nice to be able to tell the IDE not to do this.

For reference, this results in the following input being fed to the compiler:

#line 1 "Silly002.ino"
#include "Arduino.h"
void myfunc(funcptr f); // THIS IS WHAT CAUSES THE ERROR TO OCCUR -- AUTOMATICALLY INSERTED BY IDE
void setup();
void loop();
#line 1
typedef void (*funcptr)();

void myfunc(funcptr f) {
  
}

void setup() {
  
}

void loop() {
  
}

Thanks. You posted just as I was hitting post.

This is one of the silly, annoying things that the so-called "IDE" does.

One work-around is not to put anything but #includes in the "sketch" file.

Nick Gammon covers this here: http://www.gammon.com.au/forum/?id=12625

Probably a better solution for someone with your level of competence is not to use the pseudo-IDE at all.

I recently had my fill of doing battle with the IDE and installed a Makefile to build projects and I now use Geany an excellent, syntax aware, code editor. I covered this here: http://forum.arduino.cc/index.php?topic=336098.msg2320167#msg2320167

That is closer to what I regard as development environment. You can click on a compiler error and open the source file with the cursor on the error line.

The Arduino as an AVR hardware base, though rather long in the tooth now, has merit, but the IDE is best suited to novice level, LED blinking type projects IMO.

HTH.

It is a useful feature because it removes one common gotcha that the naive user really shouldn't have to care/know about (declaring every function before use).

Once you are doing more complex stuff it can bite you, but the whole ethos of the Arduino is make things simple and easy as possible for non programmers. You have the source code to the software if you want to make your own modifications, such as a preference to enable/disable this feature (check there isn't one already!)

"It is a useful feature because it removes one common gotcha that the novice driver
really shouldn’t have to care/know about ( knowing how to drive a car.) "

aka dumbing down the user. :frowning:

Making things “simpler” by making things more complicated has never worked for me.

The lunacy of Arduino is: if someone is still at the legoland level of programming what the hell are they doing working in C / C++ ???

For high performance , bare-metal, programming it is a good choice. For blinking LEDs it’s probably among the worst.