Current location - Loan Platform Complete Network - Big data management - C++ Type Conversion Rules, When Implicit Type Conversion Occurs and Where Forced Type Conversion Applies
C++ Type Conversion Rules, When Implicit Type Conversion Occurs and Where Forced Type Conversion Applies

C++ is a strongly typed language that requires compile-time type declarations and checking, and consistency between the types of operands in an expression (including the left and right values of assignments, and formal and real parameters), but also allows for some flexibility, and a certain degree of compatibility, which means that types can be implicitly converted and forced to be converted according to certain rules.

The type of the operand(s) determines whether an expression is legal and, if the expression is legal, determines the meaning of the expression. However, in C++ some types are related to one another. When two types are related, we can use an object or value of one type where an operand of the related type is expected. When two types are related, we can use an object or value of one type where an operand of the related type is expected. Two types are related if there is a conversion between them.

Whether or not an expression is legal depends on the type of the operand, and the meaning of a legal expression is determined by the type of its operand. However, in C++, there are related dependencies between certain types. If two types are related, an object or value of a related type of a certain type can be used at an operand location that requires that type. Two types are said to be related if they are convertible to each other.

1.1 The Arithmetic Conversions

The language defines a set of conversions among the built-in types. Among these, the most common are the conversions, the conversions, and the conversions. the most common are the arithmetic conversions , which ensure that the two operands of a binary operator, such as an arithmetic or logical operator, are converted to a common type before the operator is evaluated. That common type is also the result type of the expression.

The C++ language provides a set of conversions for built-in types. C++ provides a set of conversion rules for built-in types, the most common of which is arithmetic conversion. The most common of these is arithmetic conversion, which ensures that the two operands of a binary operator (such as an arithmetic or logical operator) are converted to the same type before the operation is performed, and that the value of the expression is also of the same type.

The rules define a hierarchy of type conversions in which operands are converted to the widest type in the expression. The conversion rules are defined so as to preserve the precision of the values involved in a multi-type expression. For example, if one operand is of type long double, then the other The conversion rules are defined so as to preserve the precision of the values involved in a multi-type expression. For example, if one operand is of type long double, then the other is converted to type long double regardless of what the second type is.

The arithmetic conversion rules define a hierarchy of type conversions that specifies the order in which operands should be converted to the widest type in the expression. In expressions that contain multiple types, the conversion rules ensure the precision of the computed values. For example, if one operand is of type long double, it will be converted to long double regardless of the type of the other operand.

The simplest kinds of conversion are integral promotions . Each of the integral types that are smaller than int char, signed char, unsigned char, short, and unsigned shortis promoted to int if all possible values of Each of the integral types that are smaller than int char, signed char, unsigned char, short, and unsigned shortis promoted to int if all possible values of that type fit in an int. Otherwise, the value is promoted to unsigned int. When bool values are promoted to int, a false value promotes to zero and true to one.

The simplest conversion is integer promotion: for all integer types smaller than int, including char, signed char, unsigned char, short, and unsigned short, if all possible values of that type fit in an int, they are promoted to If a bool value is promoted to int, false is converted to 0, and true is converted to 1.

Note that these conversions are only arithmetic conversions, and that storage is converted to the type it was declared as.

1.2 Conversions between Signed and Unsigned Types

When an unsigned value is involved in an expression, the conversion rules are defined to preserve the value of the operands. Conversions involving unsigned operands depend on the relative Conversions involving unsigned operands depend on the relative sizes of the integral types on the machine. Hence, such conversions are inherently machine dependent.

If an unsigned value is used in an expression, the conversion rules are defined to preserve the value of the operands. Conversions of unsigned operands depend on the relative size of integers on the machine. Hence, such conversions are inherently machine dependent.

In expressions involving shorts and ints, values of type short are converted to int. Expressions involving unsigned short are converted to int if the int type is large enough to represent all the values of an unsigned short. Otherwise, both operands are converted to unsigned int. For example, if For example, if shorts are a half word and ints a word, then any unsigned value will fit inside an int. On such a machine, unsigned shorts are converted to int.

Expressions of type short and int are converted to int if the int type is large enough to represent all the values of an unsigned short.

Contains expressions of type short and int, and values of type short are converted to int. If the int type is sufficient for all values of the unsigned short type, the unsigned short is converted to int; otherwise, both operands are converted to unsigned int. For example, if short is represented by a half word and int is represented by a word, then all the unsigned values can be contained in int, and on such a machine, unsigned short is converted to int.

The same conversion happens among operands of type long and unsigned int. The same conversion happens among operands of type long and unsigned int. The unsigned int operand is converted to long if type long on the machine is large enough to represent all the values of the unsigned int. Otherwise, both operands are converted to unsigned long.

The conversion between long and unsigned int is the same. As long as there are enough longs on the machine to represent all the values of the unsigned int, the unsigned int is converted to a long. Otherwise, both operands are converted to unsigned long.

On a 32-bit machine, long and int are typically represented in a word. On such machines, expressions involving unsigned ints and longs are converted to unsigned long.

On such machines, expressions involving unsigned ints and longs are converted to unsigned long.

On a 32-bit machine, long and int types are typically represented in a word.

Conversions for expressions involving signed and unsigned int can be surprising. In these expressions the signed value is converted to unsigned . For example, if we compare a plain int and an unsigned int, the int is first converted to unsigned. If the int happens to hold a negative value, the result For example, if we compare a plain int and an unsigned int, the int is first converted to unsigned . For example, if we compare a plain int and an unsigned int, the int is first converted to unsigned . The signed values in an expression are converted to unsigned. For example, comparing a simple variable of type int and unsigned int, the system first converts the int value to unsigned int, and if the int value happens to be negative, the result is converted in the following way, with all its side effects.

The compiler applies conversions for both built-in and class type objects as necessary. Implicit type conversions take place in the following situations:

The compiler applies type conversion rules for both built-in and class type objects as necessary. Implicit type conversions take place in the following situations:

In expressions with operands of mixed types, the types are converted to a common type:

An expression with operands of mixed types is converted to the same type:

An expression used as a condition is converted to bool:

An expression used as a condition is converted to bool type:

Conditions occur as the first operand of the conditional (? :) operator and as the operand(s) to the logical NOT (!) , logical AND (&&&&&&&&) , logical AND (&&), and logical OR (||) operators. Conditions also appear in the if, while, for, and do while statements.

The first operator in the conditional operator (? :) and the first operand in logical NOT (!), logical AND (&), and logical OR (||) operators. , logical with (&&), and logical or (||) operands are conditional expressions. Those that appear in if, while, for, and do while statements are also conditional expressions.

An expression used to initialize or assign to a variable is converted to the type of the variable:

An expression used to initialize or assign to a variable is converted to the type of the variable:

An expression is converted to the type of the variable:

An expression used to initialize or assign to a variable is converted to the type of the variable:

In addition,implicit conversions also occur during function calls.

3.1 static_cast

Any type conversion that the compiler performs implicitly can be explicitly requested by using a static_cast:

Any type conversion that the compiler performs implicitly can be explicitly requested by using a static_cast:

Such casts are useful when assigning a larger arithmetic type to a smaller type. cast informs both the reader of the program and the compiler that we are aware of and are not concerned about the potential loss of precision. Compilers often generate a warning for assignments of a larger arithmetic type to a smaller type. When we provide the explicit cast, the warning message is turned off .

Forced conversions are useful when you need to assign a larger arithmetic type to a smaller type. In this case, the forced conversion tells the reader of the program and the compiler that we know and don't care about the potential loss of precision. For assignments from a larger arithmetic type to a smaller type, the compiler usually generates a warning. When we explicitly provide forced type conversion, the warning message is turned off.

A static_cast is also useful to perform a conversion that the compiler will not generate automatically. For example, we can use a static_cast to For example, we can use a static_cast to retrieve a pointer value that was stored in a void* pointer:

A static_cast is also useful to perform a conversion that the compiler will not generate automatically. For example, the following program uses a static_cast to cast a value that was stored in a void* pointer:

When we store a pointer in a void* and then use a static_cast to cast the pointer back to its original type

When we store a pointer in a void* and then use a static_cast to cast the pointer back to its original type, we are guaranteed that the pointer value is preserved. That is, the result of the cast will be equal to the original address value.

A pointer value stored in a void* can be forcibly converted to its original pointer type through static_cast, where we are guaranteed that the pointer value is preserved. That is, the result of the forced conversion should be equal to the original address value.

3.2 const_cast

A const_cast, as its name implies, casts away the constness of its expression. example, we might have a function named string_copy that we are certain reads, but does not write, its single parameter of type char*. If we have access to the code, the best alternative would be to correct it to take a const char*. If that is not possible, we could call string_copy on a const value using a const_cast:

const_cast, as the name implies, converts a constructed expression from its const nature. For example, suppose there is the function string_copy, with a unique argument of type char*, to which we read and write only. When accessing this function, the best option is to modify it to accept arguments of type const char*. If that doesn't work, the string_copy function can be called with a const value via const_cast:

Only a const_cast can be used to cast away constness. Using any of the other three forms of cast in This case would result in a compile-time error. Similarly, it is a compile-time error to use the const_cast notation to perform any type conversion other Similarly, it is a compile-time error to use the const_cast notation to perform any type conversion other than adding or removing const.

Const properties can only be converted using const_cast. In this case, attempting to use any of the other three forms of forced conversion will result in a compile-time error. Similarly, performing any other type conversion with the const_cast operator, other than adding or removing the const property, will cause a compile-time error.

3.3 reinterpret_cast

A reinterpret_cast generally performs a low-level reinterpretation of the bit pattern of its operands.

A reinterpret_cast generally performs a low-level reinterpretation of the bit pattern of its operands.

A reinterpret_cast is inherently machine-dependent. Safely using reinterpret_cast requires complete understanding of the types involved as well as the details of how the compiler is used. well as the details of how the compiler implements the cast.

A reinterpret_cast is inherently machine-dependent. To use reinterpret_cast safely requires the programmer to completely understand the types involved as well as the details of how the compiler implements the cast.

Before the introduction of the named forced-type conversion operator, explicit forced conversions were implemented by enclosing the type in parentheses:

The effect of this cast is the same as using the reinterpret_cast notation. However, the visibility of this cast is considerably less, making it even more difficult to track down the rogue cast.

The effect of this cast is the same as using the reinterpret_cast notation.

The effect is the same as using the reinterpret_cast symbol, but this forced cast is considerably less visible, making it even more difficult to track down the rogue cast.

Depending on the types involved, an old-style cast has the same behavior as a const_cast, a static_cast, orra reinterpret_cast. When used where a static_cast or a const_cast would be legal, an old-style cast does the same conversion as the respective named cast. If neither is legal, then an old-style cast performs a reinterpret_cast. If neither is legal, then an old-style cast performs a reinterpret_cast. For example, we might rewrite the casts from the previous section less clearly using old-style notation:

The old-style mandatory conversions have the same behavior as const_cast, static_cast, and reinterpret_cast, depending on the datatype involved. Where static_cast or const_cast is legal, old-style forced conversions provide the same functionality as their named counterparts. If neither is legal, the old-style forcible conversion performs the reinterpret_cast function. For example, we can rewrite the forced conversions of the previous section in old-style notation:

By using a cast, the programmer turns off or dampens normal type-checking. We strongly recommend that programmers avoid casts and believe that most well-formed C++ programs can be written without relying on casts.

By using a cast, the programmer turns off or dampens normal type-checking. Programmers are strongly advised to avoid forced type conversions and believe that most well-formed C++ programs can be written without relying on casts.

Arithmetic conversion

A conversion from one arithmetic type to another. In the context of the binary arithmetic operators, arithmetic conversions usually attempt to preserve precision by converting a smaller type to a larger type (e.g., small integral types, such as char and short, are converted to int).

Conversion between arithmetic types. Where binary arithmetic operators are used, arithmetic conversions typically convert a smaller type to a larger type to ensure precision (e.g., small integral types, such as char and short, are converted to int).

dynamic_cast

Used in combination with inheritance and run-time type identification.

implicit conversion

A conversion that is automatically generated by the compiler. Given an expression that needs a particular type but has expression that needs a particular type but has an operand of a differing type , the compiler will automatically convert the operand to the Given an expression that needs a particular type but has an operand of a differing type , the compiler will automatically convert the operand to the desired type if an appropriate conversion exists. Suppose an expression requires a value of a particular type, but its operand is of a different type, if an appropriate conversion is defined , the compiler will automatically convert the operand to the desired type if an appropriate conversion exists.

Integral promotions

Subset of the standard conversions that take a smaller integral type to its most closely related larger type. Integral types (e.g. short, char, etc.) are promoted to int or unsigned int.

Integral promotions are a subset of the standard conversions that take a smaller integral type to its most closely related larger type. Integer types (such as short, char, etc.) are promoted to int or unsigned int.

reinterpret_cast

Interprets the contents of the operand as a different type. Inherently machine-dependent and dangerous.

Interprets the contents of the operand as a different type. Inherently machine-dependent and dangerous.

Interprets the contents of the operand as a different type.

static_cast

An explicit request for a type conversion that the compiler would do implicitly. Often used to override Often used to override an implicit conversion that the compiler would otherwise perform.

Any type conversion that the compiler implicitly performs can be explicitly done by static_cast. We often use static_cast to override an implicit conversion that the compiler would otherwise perform.

-End-

.