2024-07-08
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
(1) Auto type automatic deduction
Auto automatically deduce the type of the variable
auto does not represent an actual type, it is just a placeholder for a type declaration
auto is not a panacea that can be deduced in all scenarios. Variables declared with auto must be initialized so that the compiler can deduce its actual type and convert auto to the actual type during compilation.
When the variable is not a pointer or reference, the const and volatile keywords are not retained in the derivation result; when the variable is a pointer or reference, the const and volatile keywords are retained in the derivation result
Scenarios where auto cannot be used:
1. Auto cannot be used as a function parameter, because using auto as a parameter is equivalent to not initializing the variable, and auto cannot be exported;
2. It cannot be used to initialize non-static member variables of a class, because non-static member variables belong to objects, and the type of an object is known only after it is created;
3. You cannot use the auto keyword to define an array;
4. Template parameters cannot be derived using auto;
Auto recommended usage scenarios:
1. Used for traversal of STL containers;
2. Used for generic programming;
(2) decltype type deduction
Sometimes you don't need or can't define a variable, but you want to get a certain type. You can use decltype. decltype deduce the type of an expression during compilation, such as: int x=18; decltype(x) a=x; that is, the type of a is deduced through x.
Derivation rules:
1. The expression is a common variable, a common expression, or a time-class expression. The type deduced by decltype is consistent with the type of the expression.
2. The expression is a function, and the type deduced using decltype is consistent with the return value of the function (if the return value is modified by const or volatile qualifiers, the qualifiers are ignored);
3. The expression is an lvalue or is enclosed by ( ), and the expression type is derived by decltype (if the return value is modified by const or volatile qualifiers, the qualifiers are ignored);
Application scenarios of decltype:
1. It is mostly used in generic programming because there are a large number of uncertain types in generic programming;
final is used to restrict a class from being inherited or a virtual function from being overridden
When modifying a function with final, only virtual functions can be modified, and the final keyword must be placed after the class or function. When a virtual function is modified with final, it can prevent the subclass from overriding the virtual function of the parent class.
When final modifies a class, writing final after the class name means that this class is no longer allowed to be inherited, that is, this class no longer has derived classes.
Early role: used to declare namespaces. Using namespaces can prevent naming conflicts; user subclasses call methods of the same name in hidden parent classes;
New function: define an alias for a type. Note that using actually just gives an alias to the original type, rather than defining a new type. Usage: using new type = old type, such as: using my_int = int;
Using using instead of typedef to define function pointers makes the code easier to read. Earlier, typedef was used to define function pointers, such as: typedef int (func)(int, string); Use using definition instead: using func = int()(int, string);
Using can assign an alias to a template, but typedef cannot
Early C compilers interpreted the double right angle brackets as the right shift operator
The new feature improves the compiler's parsing rules to parse multiple right angle brackets into template parameter terminators as much as possible.
Override is used to ensure that the function to be overridden in the derived class has the same signature as the corresponding virtual function of the base class, and to explicitly indicate that the function of the base class will be overridden.
Override is written after the function, which explicitly declares that the function is overridden. The compiler will check the type of the function and the virtual function of the parent class.
Write the function's return value type after the function declaration body, auto func(parameter 1, parameter 2, ...) -