Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default argument in the middle of parameter list?

I saw a function declaration in our code that looked as follows

void error(char const *msg, bool showKind = true, bool exit); 

I thought first that this is an error because you cannot have default arguments in the middle of functions, but the compiler accepted this declaration. Has anyone seen this before? I'm using GCC4.5. Is this a GCC extension?

The weird thing is, if I take this out in a separate file and try to compile, GCC rejects it. I've double checked everything, including the compiler options used.

like image 245
Johannes Schaub - litb Avatar asked Apr 12 '11 15:04

Johannes Schaub - litb


People also ask

Where must default parameter be in the argument list?

The default constructor with argument has a default parameter x, which has been assigned a value of 0. Key Points: Default arguments are different from constant arguments as constant arguments can't be changed whereas default arguments can be overwritten if required.

What is default arguments give example?

The default arguments facilitate the function call statement with partial or no arguments. Example : defaultvalue (x, y); defaultvalue (200, 150);

What are the parameters that are default values?

The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.


2 Answers

That code would work if in the very first declaration of the function, the last parameter has default value, something like this:

//declaration void error(char const *msg, bool showKind, bool exit = false); 

And then in the same scope you can provide default values for other arguments (from right side), in the later declaration, as:

void error(char const *msg, bool showKind = true, bool exit); //okay  //void error(char const *msg = 0 , bool showKind, bool exit); // error 

which can called as:

error("some error messsage"); error("some error messsage", false); error("some error messsage", false, true); 

Online Demo : http://ideone.com/aFpUn

Note if you provide default value for the first parameter (from left), without providing default value for the second, it wouldn't compile (as expected) : http://ideone.com/5hj46


§8.3.6/4 says,

For non-template functions, default arguments can be added in later declarations of a function in the same scope.

Example from the Standard itself:

void f(int, int); void f(int, int = 7); 

The second declaration adds default value!

Also see §8.3.6/6.

like image 169
Nawaz Avatar answered Sep 20 '22 09:09

Nawaz


The answer might be in 8.3.6:

8.3.6 Default Arguments

6 Except for member functions of class templates, the default arguments in a member function definition that appears outside of the class definition are added to the set of default arguments provided by the member function declaration in the class definition. Default arguments for a member function of a class template shall be specified on the initial declaration of the member function within the class template.

Example:

class C { void f(int i = 3); void g(int i, int j = 99); }; void C::f(int i = 3) // error: default argument already { } // specified in class scope void C::g(int i = 88, int j) // in this translation unit, { } // C::g can be called with no argument 

After reading this, I found that MSVC10 accepted the following with compiler extensions turned off:

void error(char const* msg, bool showKind, bool exit = false);  void error(char const* msg, bool showKind = false, bool exit) {     msg;     showKind;     exit; }  int main() {     error("hello"); } 
like image 20
John Dibling Avatar answered Sep 18 '22 09:09

John Dibling