Default arguments:
Some functions have such a formal parameter that they are given the same value in many calls to the function, and we call this recurring value the default argument of the function. When you call a function that contains a default argument, you can include the argument or omit the argument.
Special attention is paid to the following:
1. Once a parameter is given a default value, all parameters following it must have a default value.
2. Only the trailing arguments can be omitted.
3. Participants start matching from the first parameter of the formal parameter list, and can match successfully if their type can be converted to each other.
4. A local variable cannot be the default argument. In addition, the expression can be used as the default argument as long as the type of the expression can convert the type required by the forming parameter.
1#include <iostream>2 using namespacestd;3 4 intGelintA =1,intb =1,intc =1){5 returnA + B +C;6 }7 8 intTingintA =1,Charb ='a',stringc ="FJSLF"){9 returnA;Ten } One A intYy1 =1, Yy2 =1; - - intYuintA = Yy1,intb = yy2) {//You can use global variables as default arguments the returnA +b; - } - - //int lou (int a = 1, int b, int c) {//error: Once a parameter has a default value, all parameters following it must have a default value + // ; - // } + A intMainvoid){ at intCnt1 =gel (); - intCnt2 = Gel (2); - intCnt3 = Gel (2,2); - intCnt4 = Gel (2,2,2); -cout << cnt1 <<" "<< Cnt2 <<" "<< Cnt3 <<" "<< cnt4 << Endl;//Output 3 4 5 6 - in intCC1 = Ting (Ten,'a',"FSKL"); - //int cc2 = ting (' A ', "LSJF");//error: Only trailing arguments can be omitted to intCC3 = Ting (2,'b'); + intCC4 = Ting ('b');//Note that ' B ' can be converted to type int, at which point ' B ' is assigned to parameter a -cout << cc1 <<" "<< CC3 <<" "<< cc4 << Endl;//Output 2 98 the return 0; *}
Inline functions:
Inline functions can avoid the time overhead of function calls. Specifies that a function is an inline function, usually by expanding it inline on the calling point. Equivalent to embedding the function directly into the call point.
Defines an inline function that compares two int types:
1 bool compre (constintconstint b) {2 return a > b; 3 }
The inline description is just a request to the compiler, and the compiler can choose to ignore the request. In general, the inline mechanism is used to optimize functions that are small in size, straightforward in process, and frequently called. Many compilers do not support inline recursive functions, and a 75-line function is unlikely to be expanded inline within the call point.
constexpr function:
The constexpr function refers to a function that can be used for constant expressions. There are several conventions that define the methods of the CONSTEXPR function and other function types:
1. The return type of the function and all the formal parameter types are literal types.
2. The function body must have only one return statement.
1#include <iostream>2 using namespacestd;3 4constexprintNEW_SZ (inta) {5 returnA;6 }7 8constexprCharNew_char (Chars) {9 returns;Ten } One A //constexpr string New_str (string s) {//Error - //return s; - // } the - intMainvoid){ - Const intFoo = NEW_SZ (Ten); -cout << Foo <<Endl; + - Const Charch = New_char ('h'); +cout << Ch <<Endl; A return 0; at}
When the initialization task is performed, the compiler replaces the call to the CONSTEXPR function with its result value. To be able to expand at any time during the compilation process, the CONSTEXPR function is implicitly specified as an inline function.
The constexpr function body can also contain other statements, as long as these statements do nothing at run time. For example: The CONSTEXPR function can have empty statements, type aliases, and using declarations:
1constexprintNEW_SZ (inta) {2 //int b; Error: cannot have a statement that executes an action in the CONSTEXPR function3 usingLL =Long Long;//you can have a using declaration in the CONSTEXPR function4;//you can have empty statements in the CONSTEXPR function5typedefint inch;//you can have a named type alias in the CONSTEXPR function6 returnA;7}
Unlike other functions, inline functions and constexpr functions can be defined more than once in a program. After all, it is not enough for the compiler to have a declaration to expand a function, but also to define a function. However, for a given inline function or constexpr function, its multiple definitions must be exactly the same. For this reason, inline functions and constexpr functions are usually defined in the header file.
Debugging Help
ASSERT preprocessing macros:
Special-Purpose language features