Const_cast vs reinterpret_cast

Referring to the SO C ++ FAQ When should static_cast, dynamic_cast and reinterpret_cast be used? .

const_cast is used to remove or add a constant to a variable and its only reliable, defined and legal way to remove a constant. reinterpret_cast is used to change the interpretation of a type.

I understand in a reasonable way why a constant variable should be cast non-const only with const_cast, but I cannot find a reasonable justification for the problems using reinterpret_cast instead of const_cast to add a constant.

I understand that using reinterpret_cast to even add a constant is not normal, but will it be a UB or a potential time bomb to use reinterpret_cast to add a constant?

The reason I was confused is due to the expression

In many ways, the only guarantee you get with reinterpret_cast is that if you return the result back to the original type, you will get the exact same value.

So, if I add a constant using reinterpret_cast, and if you reinterpret the result returning to the original type, it should return to the original type and should not be UB, but this violates the fact that you need to use const_cast to remove the constant

In a separate note, the standard ensures that you can add Constness using the reinterpret case

5.2.10 Re-interpret casting (7) ... When a v value of type "pointer to T1" is converted to type "pointer to cv T2", the result is static_cast (static_cast (v)) if both T1 and T2 are equal to standard formats (3.9) and the alignment requirements of T2 are no more stringent than that of T1 ........

+6
source share
5 answers

reinterpret_cast changes the interpretation of data inside an object. const_cast adds or removes the const qualifier. The data representation and constant are orthogonal. Therefore, it makes sense to have different cast keywords.

So, if I add a constant using reinterpret_cast, and if you reinterpret the result returning to the original type, it should return to the original type and should not be UB, but this violates the fact that you need to use const_cast to remove the constant

This does not even compile:

 int * n = new int; const * const_added = reinterpret_cast<const int *>(n); int * original_type = reinterpret_cast<int*>(const_added); // error: reinterpret_cast from type 'const int*' to type 'int*' casts away qualifiers 
+9
source

You should not just add const with reinterpret_cast . A reinterpret_cast should be primarily that: reinterpret the pointer (or something else).

In other words, if you go from const char* to char* (I hope because there is a bad API that you cannot change), then const_cast is your friend. That is really all he intended.

But if you need to switch from MyPODType* to const char* , you need reinterpret_cast , and it's just nice if you don't need const_cast on top of it.

+3
source

One thing to keep in mind: you cannot use const_cast to make the const variable writable. You can only use it to get a non-const reference from a const reference if that const reference refers to a non-constant object. Sounds complicated? Example:

 // valid: int x; int const& x1 = x; const_cast<int&>(x1) = 0; // invalid: int const y = 42; int const& y1 = y; const_cast<int&>(y1) = 0; 

In fact, both of them will compile, and sometimes even "work." However, the second causes undefined behavior and in many cases terminates the program when a permanent object is placed in read-only memory.

However, a few more things: reinterpret_cast is the most powerful actor, but also the most dangerous, so do not use it if you do not need to. When you need to switch from void* to sometype* , use static_cast . When moving in the opposite direction, use the built-in implicit conversion or use the explicit static_cast . Similar to adding or removing const , which is also added implicitly. Regarding reinterpret_cast , see Also C ++ discussion . When we prefer to use the two-dimensional static_cast over reinterpret_cast , where an alternative that is less hacky is discussed.

Uli

+1
source

The only place I can think of binding reinterpret_cast to const-ness is to pass a const object to an API object that takes a pointer to void -

 UINT ThreadFunction(void* param) { const MyClass* ptr = reinterpret_cast<const MyClass*>(param); } 
0
source

Yes, as you know, const_cast means that it removes a constant from a specific type.

But, when we need to add a constant to the type. Is there a reason we should do this?

eg,

 void PrintAnything(void* pData) { const CObject* pObject = reinterpret_cast<CObject*>(pData); // below is bla-bla-bla. } 

reinterpret_cast has nothing to do with 'const'.

const_cast means two things. The first is to remove the constant from one type, and the other to show its code. Because you can use it using the C-style, but this is clearly not the case, which is not recommended.

They do not work the same. it is definitely different.

0
source

All Articles