C++11 - processing type

Type alias:

A type alias is a name that is synonymous with a type.

Definition of type alias

Method 1 (traditional): keyword typedef

Example:

typedef double wages;    //Waves is a synonym for double;
typedef wages base, *p;    //base is a synonym for waves (double); p is a synonym for double *;

Method 2 (a new method specified in C++11): use alias declaration

Use the keyword using as the beginning of the alias declaration, followed by the alias and the equal sign. The function is to specify the name on the left of the equal sign as the alias of the type on the right of the equal sign.
Example:

using SI = Sales_item;

Note: the type alias cannot be completely replaced with the original sentence it refers to for ease of understanding.
If a type alias refers to a pointer type, for example:

typedef char *pstring;    //pstring is an alias of type char *
const pstring cstr = 0;    //cstr is a constant pointer to char type!
here const pstring It's pointing char Constant pointer of type,


If pstring is replaced by char *, that is

const char *cstr = 0;


It will be understood as a pointer to const char type, which is wrong!
Because after rewriting with char *, the data type becomes char, * becomes a part of the declarator, and const char becomes the basic data type instead of a pointer;
The basic data type of pstring is declared as pointer type in the declaration statement, so const pstring is a constant pointer type, which is equivalent to char *const cstr = 0;

auto type specifier

The new annotation of C++11 introduces auto type specifier to solve the problem that the type of initialization expression cannot be clearly known when declaring variables. Auto allows the compiler to calculate the type of variable through the initial value, so the variable defined by auto must have an initial value.
Example:

double val1 = 1.5, val2 = 2.5;
auto item = val1 + val2;    //Correct: item is initialized to the result of the addition of val1 and val2
auto i = 0, *p = &i;        //Correct: i is an integer and p is an integer pointer
auto sz = 0, pi = 3.14;    //Error: sz and pi are of different types


The auto type inferred by the compiler is sometimes different from the type of the initial value. The compiler will appropriately change the result type to make it more consistent with the initialization rules.
1. The reference is used as an initial value and is initialized to the type of the reference object
Example:

int i = 0, &r = i;
auto a = r;    //a is an integer type


2. auto will ignore the top const and keep the bottom const
Example:

int i = 0;
const int ci = i, &rc =ci;
auto a = ci;    //a is of type int (the top const of ci is ignored)
auto b = rc;    //b is int
auto c = &i;    //c is an int pointer
auto d = &ci;    //d is a pointer to const int (addressing a constant object is an underlying const)

//Declare top-level const with const auto
const auto e = ci;    //e is const int

//Declare const reference type with auto
auto &f = ci;    //f is a reference of const int type
auto &g = 1024;    //Error: cannot bind constant for non constant reference
const auto &h = 1024;    //Correct: you can bind constants for constant references

decltype type indicator

The new C++11 standard introduces the second type specifier: decltype, which is used to select and return the data type of the operand.
Note:
1. If the operand is an expression, the type corresponding to the expression result is returned.
2. If the operand is a dereference operation, decltype gets the reference type.
3. If the variable name in the operand is bracketed, the compiler treats it as an expression. Variable is a special expression that can be used as the left value of assignment statement, so decltype will get the reference type.
Example:

int ci = 0, &rc = ci, *p = &ci;
decltype(ci) x = 0;    //The type of x is int
decltype(rc) y = x;    //The type of y is int &, bound to x
decltype(rc + 0) a;    //The type of a is int, because the result of addition is int (Note 1,)
decltype(*p) b;    //Error: the type of b is int &, which must be initialized when defining (Note 2,)
decltype((ci)) c;    //Error: the type of c is int &, which must be initialized when defining (Note 3,)
decltype(f()) sum =x;    //Instead of actually calling function f, the compiler uses the type of the return value of F when the call occurs as the type of sum.

Keywords: C++

Added by OttoBufonto on Tue, 21 Dec 2021 16:54:16 +0200