You are here:

C++/default arguments


i have a question about default arguments in c++.
could you plz tell me what's the implication of"Default arguments must be the rightmost (trailing) arguments in a function's parameter list"
actually i don't know about rightmost(trailling) arguments.


Any parameters after a parameter having a default argument value must have default argument values. That is, you canít have a default argument followed by a non-default argument.

For example:

void foo( int a, int b = 2, int c = 3 ) ;  // ok, trailing defaults

void bar( int a = 1, int b = 2, int c ) ;  // error, leading defaults, c must have a default

void baz( int a, int b = 3, int c ) ;  // error, default in middle, c must have a default

Also, if you use the default for an argument in a particular function call, you must use the default for all the arguments that follow it in the functionís argument list.

For example:

void foo( 7 ) ;  // ok, a specified, b and c are defaulted

void foo( 7, 23 ) ;  // ok, a and b specified, c is defaulted

void foo( 7, , 23 ) ;  // error, b is defaulted, c must also be defaulted  


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2016 All rights reserved.