Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

casting to the same type

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?

like image 441
Humam Helfawi Avatar asked Aug 17 '16 05:08

Humam Helfawi


People also ask

What is type casting example?

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.

What is meant by casting of data types?

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.

Is type conversion and type casting same?

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.

What is type cast operator?

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.


1 Answers

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 expression v to type T.

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.

like image 128
R Sahu Avatar answered Oct 21 '22 03:10

R Sahu