9

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........

4
  • @clossvoters: Can you please refer me to the duplicate question that answers this?
    – Abhijit
    Jan 11, 2013 at 6:08
  • 1
    const_cast also covers addition/removal of volatile. Jan 11, 2013 at 6:09
  • @MikeDeSimone: Yes definitely, but my current focus is only constness
    – Abhijit
    Jan 11, 2013 at 6:10
  • 1
    That statement should read "...if you reinterpret_cast the result back to the original type..."
    – K-ballo
    Jan 11, 2013 at 6:15

5 Answers 5

16

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.

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

That wouldn't 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
2
  • 1
    Refer 5.2.10 Reinterpret cast: When a prvalue v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast<cv T2*>(static_cast<cv void*>(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.. So it seems you can add constness using reinterpret cast.
    – Abhijit
    Jan 12, 2013 at 4:54
  • 2
    Non-const objects can always be implicitly converted to const without requiring any cast whatsoever. I don't see why reinterpret_cast should be an exception to this rule. Jan 12, 2013 at 5:58
6

You shouldn't just be adding const with reinterpret_cast. A reinterpret_cast should be primarily that: reinterpreting the pointer (or whatever).

In 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.

But 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.

2

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:

// 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 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.

That 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.

Uli

0

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 -

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

yeah, as you know, const_cast means that it removes constness from a specific type.

But, when we need to add constness to a type. Is there a reason we have to do it?

for example,

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. first one is to remove constness from a type and the other is to give its code explicitness. Because you can use cast it using C-style cast, but this is not explicit so that is not recommended.

They do not function same. it is definitely different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.