Cpp function default argument

Default arguments

Allows a function to be called without providing one or more trailing arguments.

Indicated by using the following syntax for a parameter in the parameter-list of a function declaration.

attr (optional) decl-specifier-seq declarator = initializer (1)
attr (optional) decl-specifier-seq abstract-declarator (optional) = initializer (2)

Default arguments are used in place of the missing trailing arguments in a function call:

void point(int x = 3, int y = 4); point(1, 2); // calls point(1, 2) point(1); // calls point(1, 4) point(); // calls point(3, 4)

In a function declaration, after a parameter with a default argument, all subsequent parameters must:

int x(int = 1, int); // Error: only the trailing arguments can have default values // (assuming there's no previous declaration of x) void f(int n, int k = 1); void f(int n = 0, int k); // OK: k's default supplied by previous decl in the same scope void g(int, int = 7); void h() { void g(int = 1, int); // Error: not the same scope }
templateclass. T> struct C { void f(int n = 0, T. ); }; Cint> c; // OK; instantiates declaration void C::f(int n = 0, int)
templateclass. T> void h(int i = 0, T. args); // OK

The ellipsis is not a parameter, and so can follow a parameter with a default argument:

Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++11) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations. Template parameter lists use similar syntax for their default template arguments.

For non-template functions, default arguments can be added to a function that was already declared if the function is redeclared in the same scope. At the point of a function call, the defaults are a union of the defaults provided in all visible declarations for the function. A redeclaration cannot introduce a default for an argument for which a default is already visible (even if the value is the same). A re-declaration in an inner scope does not acquire the default arguments from outer scopes.

void f(int, int); // #1 void f(int, int = 7); // #2 OK: adds a default void h() { f(3); // #1 and #2 are in scope; makes a call to f(3,7) void f(int = 1, int); // Error: inner scope declarations don't acquire defaults } void m() { // new scope begins void f(int, int); // inner scope declaration; has no defaults. f(4); // Error: not enough arguments to call f(int, int) void f(int, int = 6); f(4); // OK: calls f(4,6); void f(int, int = 6); // Error: cannot redeclare a default in the same scope } void f(int = 1, int); // #3 OK, adds a default to #2 void n() { // new scope begins f(); // #1, #2, and #3 are in scope: calls f(1, 7); }

If an inline function is declared in different translation units, the accumulated sets of default arguments must be the same at the end of each translation unit.

If a non-inline function is declared in the same namespace scope in different translation units, the corresponding default arguments must be the same if present (but some default arguments can be absent in some TU).

If a friend declaration specifies a default, it must be a friend function definition, and no other declarations of this function are allowed in the translation unit.

The using-declaration carries over the set of known default arguments, and if more arguments are added later to the function’s namespace, those defaults are also visible anywhere the using-declaration is visible:

namespace N { void f(int, int = 1); } using N::f; void g() { f(7); // calls f(7, 1); f(); // error } namespace N { void f(int = 2, int); } void h() { f(); // calls f(2, 1); }

The names used in the default arguments are looked up, checked for accessibility, and bound at the point of declaration, but are executed at the point of the function call:

int a = 1; int f(int); int g(int x = f(a)); // lookup for f finds ::f, lookup for a finds ::a // the value of ::a, which is 1 at this point, is not used void h() { a = 2; // changes the value of ::a { int a = 3; g(); // calls f(2), then calls g() with the result } }

For a member function of a non-template class, the default arguments are allowed on the out-of-class definition, and are combined with the default arguments provided by the declaration inside the class body. If these out-of-class defaults would turn a member function into a default constructor or copy /move (since C++11) constructor/assignment operator, the program is ill-formed. For member functions of class templates, all defaults must be provided in the initial declaration of the member function.

class C { void f(int i = 3); void g(int i, int j = 99); C(int arg); // non-default constructor }; void C::f(int i = 3) {} // error: default argument already // specified in class scope void C::g(int i = 88, int j) {} // OK: in this translation unit, // C::g can be called with no argument C::C(int arg = 1) {} // Error: turns this into a default constructor

The overriders of virtual functions do not acquire the default arguments from the base class declarations, and when the virtual function call is made, the default arguments are decided based on the static type of the object (note: this can be avoided with non-virtual interface pattern).

struct Base { virtual void f(int a = 7); }; struct Derived : Base { void f(int a) override; }; void m() { Derived d; Base& b = d; b.f(); // OK: calls Derived::f(7) d.f(); // Error: no default }

Local variables are not allowed in default arguments unless they are not evaluated:

void f() { int n = 1; extern void g(int x = n); // error: local variable cannot be a default extern void h(int x = sizeof n); // OK as of CWG 2082 }

The this pointer is not allowed in default arguments:

class A { void f(A* p = this) {} // error: this is not allowed };

Non-static class members are not allowed in default arguments (even if they are not evaluated), except when used to form a pointer-to-member or in a member access expression:

int b; class X { int a; int mem1(int i = a); // error: non-static member cannot be used int mem2(int i = b); // OK: lookup finds X::b, the static member int mem3(int X::* i = &X::a); // OK: non-static member can be used int mem4(int i = x.a); // OK: in a member access expression static X x; static int b; };

A default argument is evaluated each time the function is called with no argument for the corresponding parameter. Function parameters are not allowed in default arguments except if they are not evaluated. Note that parameters that appear earlier in the parameter list are in scope:

int a; int f(int a, int b = a); // Error: the parameter a used in a default argument int g(int a, int b = sizeof a); // Error until resolving CWG 2082 // OK after resolution: use in unevaluated context is OK

The default arguments are not part of the function type:

int f(int = 0); void h() { int j = f(1); int k = f(); // calls f(0); } int (*p1)(int) = &f; int (*p2)() = &f; // Error: the type of f is int(int)

Operator functions cannot have default arguments, except for the function call operator:

class C { int operator[](int i = 0); // ill-formed int operator()(int x = 0); // OK };

Explicit object parameters cannot have default arguments:

struct S { void f(this const S& = S{}); }; // ill-formed

[edit] Note

Spaces may be necessary to avoid a compound assignment token if the parameter name is absent.

void f1(int*=0); // Error, '*=' is unexpected here void g1(const int&=0); // Error, '&=' is unexpected here void f2(int* = 0); // OK void g2(const int& = 0); // OK void h(int&&=0); // OK even without spaces, '&&' is a token here

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 217 C++98 a default argument could be added to a non-
template member function of a class template
prohibited
CWG 1344 C++98 default arguments added in the out-of-class definition of a
member function could change it to a special member function
prohibited
CWG 1716 C++98 default arguments were evaluated each time the function is called,
even if the caller provided the arguments
evaluated only if no
argument is provided for the
corresponding parameter
CWG 2082 C++98 default arguments were forbidden to use local variables
and preceding parameters in unevaluated context
unevaluated context
use allowed
CWG 2233 C++11 parameters expanded from parameter packs could
not appear after parameters with default arguments
allowed

Источник

Default Arguments

In many cases, functions have arguments that are used so infrequently that a default value would suffice. To address this, the default-argument facility allows for specifying only those arguments to a function that are meaningful in a given call. To illustrate this concept, consider the example presented in Function Overloading.

// Prototype three print functions. int print( char *s ); // Print a string. int print( double dvalue ); // Print a double. int print( double dvalue, int prec ); // Print a double with a // given precision. 

In many applications, a reasonable default can be supplied for prec , eliminating the need for two functions:

// Prototype two print functions. int print( char *s ); // Print a string. int print( double dvalue, int prec=2 ); // Print a double with a // given precision. 

The implementation of the print function is changed slightly to reflect the fact that only one such function exists for type double :

// default_arguments.cpp // compile with: /EHsc /c // Print a double in specified precision. // Positive numbers for precision indicate how many digits // precision after the decimal point to show. Negative // numbers for precision indicate where to round the number // to the left of the decimal point. #include #include using namespace std; int print( double dvalue, int prec ) < // Use table-lookup for rounding/truncation. static const double rgPow10[] = < 10E-7, 10E-6, 10E-5, 10E-4, 10E-3, 10E-2, 10E-1, 10E0, 10E1, 10E2, 10E3, 10E4, 10E5, 10E6 >; const int iPowZero = 6; // If precision out of range, just print the number. if( prec >= -6 && prec 

To invoke the new print function, use code such as the following:

print( d ); // Precision of 2 supplied by default argument. print( d, 0 ); // Override default argument to achieve other // results. 

Note these points when using default arguments:

    Default arguments are used only in function calls where trailing arguments are omitted — they must be the last argument(s). Therefore, the following code is illegal:

int print( double dvalue = 0.0, int prec ); 
// Prototype for print function. int print( double dvalue, int prec = 2 ); . // Definition for print function. int print( double dvalue, int prec = 2 )

Источник

Аргументы по умолчанию

Во многих случаях функции имеют аргументы, которые используются настолько редко, что достаточно значения по умолчанию. В таких случаях возможность задания аргументов по умолчанию позволяет указывать только те аргументы функции, которые важны в конкретном вызове. Чтобы проиллюстрировать эту концепцию, рассмотрим пример, представленный в разделе Перегрузка функций.

// Prototype three print functions. int print( char *s ); // Print a string. int print( double dvalue ); // Print a double. int print( double dvalue, int prec ); // Print a double with a // given precision. 

Во многих приложениях для аргумента prec можно задать разумное значение по умолчанию, что исключает необходимость наличия двух функций:

// Prototype two print functions. int print( char *s ); // Print a string. int print( double dvalue, int prec=2 ); // Print a double with a // given precision. 

Реализация функции немного изменена print , чтобы отразить тот факт, что для типа double существует только одна такая функция :

// default_arguments.cpp // compile with: /EHsc /c // Print a double in specified precision. // Positive numbers for precision indicate how many digits // precision after the decimal point to show. Negative // numbers for precision indicate where to round the number // to the left of the decimal point. #include #include using namespace std; int print( double dvalue, int prec ) < // Use table-lookup for rounding/truncation. static const double rgPow10[] = < 10E-7, 10E-6, 10E-5, 10E-4, 10E-3, 10E-2, 10E-1, 10E0, 10E1, 10E2, 10E3, 10E4, 10E5, 10E6 >; const int iPowZero = 6; // If precision out of range, just print the number. if( prec >= -6 && prec 

Чтобы вызвать новую функцию print , используйте следующий код:

print( d ); // Precision of 2 supplied by default argument. print( d, 0 ); // Override default argument to achieve other // results. 

При использовании аргументов по умолчанию обратите внимание на следующие моменты:

    Аргументы по умолчанию используются только в вызовах функции, в которых опущены заключительные аргументы — они должны быть последними аргументами. Поэтому следующий код недопустим:

int print( double dvalue = 0.0, int prec ); 
// Prototype for print function. int print( double dvalue, int prec = 2 ); . // Definition for print function. int print( double dvalue, int prec = 2 )

Источник

Читайте также:  Java this application cannot be run in
Оцените статью