c++
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........
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
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.
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
链接地址: http://www.djcxy.com/p/4914.html上一篇: Haswell / Skylake上的部分寄存器如何执行? Writi
下一篇: C ++