Skip to content

Prototypes for methods with default arguments are not generated [imported] #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cmaglie opened this issue Nov 15, 2012 · 4 comments
Closed
Assignees
Labels
Component: Preprocessor The Arduino sketch preprocessor converts .ino files into C++ code before compilation Type: Wontfix Arduino has decided that it will not resolve the reported issue or implement the requested feature
Milestone

Comments

@cmaglie
Copy link
Member

cmaglie commented Nov 15, 2012

This is Issue 386 moved from a Google Code project.
Added by 2010-10-23T13:23:50.000Z by [email protected].
Please review that bug for more context and additional comments, but update this bug.

Original labels: Type-Defect, Priority-Medium, Component-PreProcessor

Original description

Steps to reproduce the issue:

STEP 1. Insert the following code into a blank sketch:

void setup() {
  test(42);
  test2(42);
}

void loop() {}
void test(int arg) {};
void test2(int arg = 0) {};

STEP 2: Click the verify button and observe the error message:

DefaultArgBugDemo.cpp: In function 'void setup()':
DefaultArgBugDemo:2: error: 'test2' was not declared in this scope

STEP 3:
Examine the generated *.cpp file and observe that a prototype has been generated for test1 but not test2:

#include "WProgram.h"
void setup();
void loop();
void test(int arg);
void setup() {
  test(42);
  test2(42);
}

void loop() {}
void test(int arg) {};
void test2(int arg = 0) {};

What is the expected output? What do you see instead?

You would expect a prototype to be generated for test2.

What version of the Arduino software are you using? On what operating
system? Which Arduino board are you using?

Tested in 0018 and 0021. Running on Win7 Home Premium (though the issue likely exists on all platforms)

Please provide any additional information below.

Of course the workaround is relatively simple (just define the prototype yourself) but it would be nice for all methods to be treated equally.

@ffissore
Copy link
Contributor

Please checkout this thread [1] on devs mailing list and try one of the linked IDEs. It should fix the problems reported in this issue
[1] https://groups.google.com/a/arduino.cc/forum/#!topic/developers/4X2T3rCxXWM

@ffissore ffissore modified the milestone: Release 1.6.5 Jun 5, 2015
@ffissore
Copy link
Contributor

With #3779 the preprocessed code is now

#include <Arduino.h>
#line 1
#line 1 "sketch_sep11c.ino"
void setup();
void loop();
void test(int arg);
void test2(int arg = 0);
#line 1
void setup() {
  test(42);
  test2(42);
}

void loop() {}
void test(int arg) {};
void test2(int arg = 0) {};

which now fails with

sketch_sep11c.ino: In function 'void test2(int)':
sketch_sep11c.ino:8:23: error: default argument given for parameter 1 of 'void test2(int)' [-fpermissive]
sketch_sep11c.ino:4:6: error: after previous specification in 'void test2(int)' [-fpermissive]
default argument given for parameter 1 of 'void test2(int)' [-fpermissive]

Can you help spot the error? Code looks good to me

@ffissore ffissore added this to the Release 1.6.6 milestone Sep 11, 2015
@cmaglie
Copy link
Member Author

cmaglie commented Sep 11, 2015

The default argument must be defined only once per compilation unit, for example you can't write:

void test(int a=0);
void test(int a=0) { /* empty */ }

even if the default argument = 0 is the same.
To fix it, it seems that you can arbitrarily choose to drop the default argument in the prototype or in the implementation, I've tried to compile with g++:

void test(int a);
void test(int a=0) { /* empty */ }

and

void test(int a=0);
void test(int a) { /* empty */ }

and both compiles fine.

@ffissore
Copy link
Contributor

Now I remember an old chat about it. I chose not to handle default args. Reason is that the correct way of using default args is to have the prototype list them, as in @cmaglie second example

void test(int a=0);
void test(int a) { /* empty */ }

However this implies that preprocessor must change original source code: I mean not just inserting code, but changing existing code. This is dangerous as we don't have a source code manipulation tool powerful enough to handle all the possible cases (eg: functions defined on multiple lines, spaces, comments between arguments on multi lines...)

So this is a wontfix. If you want to use default arguments, you have to define the prototype yourself. #3779 is now smart enough to understand that a function with default args already has its prototype and leave it alone

@ffissore ffissore added the Type: Wontfix Arduino has decided that it will not resolve the reported issue or implement the requested feature label Sep 14, 2015
tbowmo pushed a commit to tbowmo/Arduino that referenced this issue Jul 14, 2016
Add before() for initialisations before radio hogs SPI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Preprocessor The Arduino sketch preprocessor converts .ino files into C++ code before compilation Type: Wontfix Arduino has decided that it will not resolve the reported issue or implement the requested feature
Projects
None yet
Development

No branches or pull requests

3 participants