I have this case:
using T = classA; //T could be classA and could be `classB` in other platforms.
T a;
auto x = static_cast<classB>(a);
In case that T is classA
the casting is must. In case of T is classB
the casting is redundant.
By standard, would the second casting be dropped (no more executable code) since it is not necessary?
In type casting, the compiler automatically changes one data type to another one depending on what we want the program to do. For instance, in case we assign a float variable (floating point) with an integer (int) value, the compiler will ultimately convert this int value into the float value.
Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.
In type casting, a data type is converted into another data type by a programmer using casting operator. Whereas in type conversion, a data type is converted into another data type by a compiler.
The type cast operator converts the data type of expr to the type specified by type-name : [ type-name ] expr. Explicit type conversions require the type cast operator. Implicit type conversions are performed automatically by 4Test and do not require explicit type casting.
From the C++11 Standard:
5.2.9 Static cast
1 The result of the expression
static_cast<T>(v)
is the result of converting the expressionv
to typeT
.
When the type of v
is the same as T
, the conversion is simple when T
is not a class. A decent compiler should not generate any executable code for such usages of static_cast
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With