Why Should Default Parameters Be Added Last in C++ Functions

Why should default parameters be added last in C++ functions?

To simplify the language definition and keep code readable.

void foo(int x = 2, int y);

To call that and take advantage of the default value, you'd need syntax like this:

foo(, 3);

Which was probably felt to be too weird. Another alternative is specifying names in the argument list:

foo(y : 3);

A new symbol would have to be used because this already means something:

foo(y = 3); // assign 3 to y and then pass y to foo.

The naming approach was considered and rejected by the ISO committee because they were uncomfortable with introducing a new significance to parameter names outside of the function definition.

If you're interested in more C++ design rationales, read The Design and Evolution of C++ by Stroustrup.

C default arguments

Not really. The only way would be to write a varargs function and manually fill in default values for arguments which the caller doesn't pass.

Default parameters in C

Default parameters is a C++ feature.

C has no default parameters.

Default values on arguments in C functions and function overloading in C

No, Standard C does not support either. Why do you feel you need to convert your C++ code to C? That could get quite tricky - I'd have thought writing wrappers would be the way to go, if your C++ must be callable from C.

Must default function parameters be constant in C++?

Default parameter can be a subset of the full set of expressions. It must be bound at compile time and at the place of declaration of the default parameter. This means that it can be a function call or a static method call, and it can take any number of arguments as far as they are constants and/or global variables or static class variables, but not member attributes.

The fact that it is bound at compile time and in the place where the function is declared also means that if it makes use of a variable, that variable will be used even if a different variable shadows the original at the place of the function call.

// Code 1: Valid and invalid default parameters
int global = 0;
int free_function( int x );

class Test
{
public:
static int static_member_function();
int member_function();

// Valid default parameters
void valid1( int x = free_function( 5 ) );
void valid2( int x = free_function( global ) );
void valid3( int x = free_function( static_int ) );
void valid4( int x = static_member_function() );

// Invalid default parameters
void invalid1( int x = free_function( member_attribute ) );
void invalid2( int x = member_function() );
private:
int member_attribute;
static int static_int;
};

int Test::static_int = 0;

// Code 2: Variable scope
int x = 5;
void f( int a );
void g( int a = f( x ) ); // x is bound to the previously defined x
void h()
{
int x = 10; // shadows ::x
g(); // g( 5 ) is called: even if local x values 10, global x is 5.
}

Default parameters: can only the last argument(s) be left?

No, it is not possible in current syntax.

Where to put default parameter value in C++?

Default parameter values must appear on the declaration, since that is the only thing that the caller sees.

EDIT: As others point out, you can have the argument on the definition, but I would advise writing all code as if that wasn't true.

Why should default parameters be added last in C++ functions?

To simplify the language definition and keep code readable.

void foo(int x = 2, int y);

To call that and take advantage of the default value, you'd need syntax like this:

foo(, 3);

Which was probably felt to be too weird. Another alternative is specifying names in the argument list:

foo(y : 3);

A new symbol would have to be used because this already means something:

foo(y = 3); // assign 3 to y and then pass y to foo.

The naming approach was considered and rejected by the ISO committee because they were uncomfortable with introducing a new significance to parameter names outside of the function definition.

If you're interested in more C++ design rationales, read The Design and Evolution of C++ by Stroustrup.

C++: Adding and redefinition of default arguments in real world

If you cannot change some existing code or library and you really cannot be bothered to type the correct argument then changing the default argument for some scope could be a solution.

It seems like the kind of hack that could be useful when working with C++ code generated by some legacy tool. For example, if the generated code has always had hundreds calls into some external library using a default argument but now the default argument is no longer correct.

Default arguments have to be bound at compiled time - why?

Actually, that's not completely accurate. The restrictions are:

8.3.6 Default arguments [dcl.fct.default]

7) Local variables shall not be used in a default argument. [ Example:

void f() {
int i;
extern void g(int x = i); //error
// ...
}

—end example ]

8) The keyword this shall not be used in a default argument of a member function. [ Example:

class A {
void f(A* p = this) { } // error
};

So, this and local variables can't be used as defaults.

For example, the following is valid:

int a = 1;
int f(int);
int g(int x = f(a)); // default argument: f(::a)
void h() {
a = 2;
{
int a = 3;
g(); // g(f(::a))
}
}

g will be called with the value f(2), which isn't a compile-time constant. This is an example straight from the standard.

The reasons it's like this is the usual: there either wasn't a proposal for it, or it was rejected, deemed not necessary or too difficult to implement.



Related Topics



Leave a reply



Submit