Home:ALL Converter>const_cast vs reinterpret_cast

const_cast vs reinterpret_cast

Ask Time:2013-01-11T14:01:12         Author:Abhijit

Json Formatter

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

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

I understand in a reasonable way, why a const variable should be casted to non-const only using const_cast, but I cannot figure out a reasonable justification of issues using reinterpret_cast instead of const_cast to add constness.

I understand that using reinterpret_cast for even adding constness is not sane but would it be an UB or potential time bomb for using reinterpret_cast to add constness?

The reason I was confused here is because of the statement

Largely, the only guarantee you get with reinterpret_cast is that if you cast the result back to the original type, you will get the exact same value.

So if I add constness using reinterpret_cast and if you reinterpret_cast the result back to the original type, it should result back to the original type and should not be UB, but that violates the fact that one should only use const_cast to remove the constness

On a separate Note, the standard guarantees that You can add Constness using reinterpret case

5.2.10 Reinterpret cast (7) ......When a prvalue v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast(static_cast(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment requirements of T2 are no stricter than those of T1........

Author:Abhijit,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14272578/const-cast-vs-reinterpret-cast
Superman :

The only place where I can think of for relating reinterpret_cast with const-ness is when passing a const object to an API that accepts a void pointer -\n\nUINT ThreadFunction(void* param)\n{\n const MyClass* ptr = reinterpret_cast<const MyClass*>(param);\n}\n",
2013-01-11T06:32:03
StackedCrooked :

reinterpret_cast changes the interpretation of the data within the object. const_cast adds or removes the const qualifier. Data representation and constness are orthogonal. So it makes sense to have different cast keywords.\n\n\n So if I add constness using reinterpret_cast and if you reinterpret_cast the result back to the original type, it should result back to the original type and should not be UB, but that violates the fact that one should only use const_cast to remove the constness\n\n\nThat wouldn't even compile:\n\nint * n = new int;\nconst * const_added = reinterpret_cast<const int *>(n);\nint * original_type = reinterpret_cast<int*>(const_added);\n // error: reinterpret_cast from type ‘const int*’ to type ‘int*’ casts away qualifiers\n",
2013-01-11T06:16:11
Mike DeSimone :

You shouldn't just be adding const with reinterpret_cast. A reinterpret_cast should be primarily that: reinterpreting the pointer (or whatever).\n\nIn other words, if you're going from const char* to char* (hopefully because there's a bad API you can't change), then const_cast is your friend. That's really all it's intended to be.\n\nBut if you need to go from MyPODType* to const char*, you need reinterpret_cast, and it's just being nice by not requiring a const_cast on top of it.",
2013-01-11T06:16:52
Ulrich Eckhardt :

There is one thing to keep in mind: You can't use const_cast to make a const variable writable. You can only use it to retrieve a non-const reference from a const reference if that const reference refers to a non-const object. Sounds complicated? Example:\n\n// valid:\nint x;\nint const& x1 = x;\nconst_cast<int&>(x1) = 0;\n// invalid:\nint const y = 42;\nint const& y1 = y;\nconst_cast<int&>(y1) = 0;\n\n\nIn reality, both of these will compile and sometimes even \"work\". However, the second one causes undefined behaviour and in many cases will terminate the program when the constant object is placed in read-only memory.\n\nThat said, a few more things: reinterpret_cast is the most powerful cast, but also the most dangerous one, so don't use it unless you have to. When you need to go from void* to sometype*, use static_cast. When going the opposite direction, use the built-in implicit conversion or use an explicit static_cast, too. Similarly with adding or removing const, which is also added implicitly. Concerning reinterpret_cast, see also the discussion at C++ When should we prefer to use a two chained static_cast over reinterpret_cast where an alternative that is less hackish is discussed.\n\nUli",
2013-01-11T06:48:56
yy