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 T is classA , you must cast. In the case of T classB casting is redundant.

According to the standard, will the second casting (no more executable code) be discarded, since this is optional?

+8
c ++ c ++ 11 static-cast
source share
1 answer

From the C ++ 11 standard:

5.2.9 Static casting

1 The result of the expression static_cast<T>(v) is the result of converting the expression v to type T

If type v matches T , the conversion is simple if T not a class. A good compiler should not generate executable code for such use of static_cast .

+3
source share

All Articles