c++ - default parameters and placeholder parameters

Default parameters and placeholder parameters

1. Default parameters

C++ allows default values ​​to be assigned to function parameters, that is, when calling the function, the values ​​of some parameters can be omitted, and the compiler will automatically pass the default values ​​to the calling statement.

1. Basic grammar

void fun01(int a = 1, int b = 2){
    
    
	cout << a << " "<< b << endl;
}
int main(){
    
    
	fun01(1, 2);
	fun01(1, 3);I
	fun01(14);
	fun01(1);
	fun01();
	return 0;
}

2. Rules for default parameters

①The default value can only be set in the declaration

② Pay attention to the parameter passing rules. Passing actual parameters to formal parameters is done from front to back .

③Set default values ​​for function parameters and set them from back to front.

④Default parameters can only be specified when the function is declared or defined, and cannot be specified again after the function is declared.

⑤ If the function has multiple parameters with default values, then when you omit a parameter, only the parameter on the right will use the default value.

⑥If there are actual parameters passed in, the actual parameters will be used. If there are no actual parameters passed in, the default parameters will be used.

3. Things to note

Although default parameters can make function calls more concise, using them too much can lead to code that is difficult to understand. In addition, when a function is overloaded, there are multiple functions with different default parameters, which may cause the compiler to be unable to determine which function should be called, thus causing an ambiguity error.

2. Placement parameters

A placeholder parameter is a special parameter used in a function template to instruct the compiler to determine the parameter type at compile time rather than specifying it when the function is called. Placeholder parameters are typically used to declare function templates where the parameter types are inferred by the compiler at instantiation time.

basic grammar

Ordinary function

#include <iostream>
using namespace std;
void fun(int a, int){
    
    //第二个参数用来占位,也可以赋默认参数
	void teste1()
	fun(1,2);fun(2);
}
	int main(){
    
    
	return 0;
}

The placeholder parameter is set for the function parameter. You need to pass the parameter when calling. You can also set the placeholder parameter as the default parameter: the
placeholder parameter will be used when symbol overloading++

Function template
placeholder parameters are declared in the function template using the typename or class keyword and represented by a placeholder identifier (usually a single-letter identifier).

template <typename T>
T add(T a, T b) {
    
    
    return a + b;
}

int main() {
    
    
    int sum = add<int>(5, 3);  // 显示指定模板参数为int
    double result = add(4.2, 2.7);  // 编译器推断出模板参数为double
    return 0;
}

In the above example, T is a placeholder identifier used to represent the type parameters of the function template. Template parameters can be specified explicitly when a function is called, or the template parameters can be inferred by the compiler based on the actual parameter types.

Multiple placeholder parameters
A function template can have multiple placeholder parameters, allowing multiple type parameters to be specified at instantiation time.

template <typename T, typename U>
T max(T a, U b) {
    
    
    return (a > b) ? a : b;
}

int main() {
    
    
    int intMax = max(5, 3.5);  // U被推断为double,返回5
    double doubleMax = max(4.2, 2.7);  // 返回4.2
    return 0;
}

Restrictions
Placeholder parameters can be used in function templates, but not in ordinary functions. In addition, template classes and functions in the C++ standard library also use placeholder parameters to implement generics.

Guess you like

Origin blog.csdn.net/qq_57737603/article/details/132439191