Reinterpret_Cast VS. C-Style Cast

Should I use a C++ reinterpret_cast over a C-style cast?

The problem with C-Style casts is that they do a lot under the hood. See here for a detailed explanation: http://anteru.net/2007/12/18/200/

You should try to always use the C++-casts, makes life easier in the long run. The main problem with C-style casts in this case is that you could have written (char*)(&v) while with reinterpret_cast, you would need an additional const_cast, so it's a bit safer. Plus you can easily find reinterpret_cast with a regex, which is not possible for the C-style casts.

c++ difference between reinterpret cast and c style cast

reinterpret_cast and const_cast are ways of getting around the C++ type system. As you noted for reinterpret_cast, this usually translates to little or no assembly code.

static_cast mostly respects the C++ type system. It could convert a number from one type to another, or call a constructor, or call a conversion function. Or for a derived-to-base conversion, it might involve adding byte offsets and/or lookups into a vtable. static_cast can also bend the type system's rules by "downcasting" a pointer or reference from a non-virtual base type to a derived type, possibly subtracting a byte offset.

And then there are pointers-to-member. They're probably beside the point here, but static_cast does things to them more or less analogous to class pointer conversions.

dynamic_cast respects the C++ type system even more strictly. In its useful form, it checks at runtime whether or not a pointer/reference actually points/refers to an object of the specified type. It typically calls a magic library function under the covers.

A function-style cast with one argument has exactly the same effect as a C-style cast. (With more than one argument, a function-style cast must be a temporary initialization using a class constructor.) A C-style cast does the first thing that makes sense out of the following list:

  • a const_cast
  • a static_cast
  • a static_cast and then a const_cast
  • a reinterpret_cast, or
  • a reinterpret_cast and then a const_cast

One exception: C-style casts can ignore private and protected inheritance relations between classes, pretending they have a public inheritance relation instead.

C-style casts are usually not preferred in C++ because it's less specific about what you want to happen.

reinterpret_cast vs c style cast

A C-style cast is equivalent to the first of the following that succeeds:

  • a const_cast
  • a static_cast
  • a static_cast followed by const_cast
  • a reinterpret_cast
  • a reinterpret_cast followed by const_cast

So in certain situations, a C-style cast will have the same effect as reinterpret_cast but they are not equivalent. Since a C-style cast is basically a "oh, just cast it however you can" cast, it's better to prefer the more specific casts.

For your example, it is preferable to use a static_cast since you know the actual type of the derived object. When you don't, use dynamic_cast.

What is the difference between static_cast and C style casting?

C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime.

Also, c++ style casts can be searched for easily, whereas it's really hard to search for c style casts.

Another big benefit is that the 4 different C++ style casts express the intent of the programmer more clearly.

When writing C++ I'd pretty much always use the C++ ones over the the C style.

Why does C style cast work but reinterpret_cast doesn't?

(In layman's terms) reinterpret_cast is used to interpret the bits of an object as another type in an implementation-defined manner. You don't want that: you want a conversion (from char to int). Use static_cast instead.

(All possible uses of reinterpret_cast are listed in 5.2.10; this is not one of them.)

Performance hit from C++ style casts?

If the C++ style cast can be conceptualy replaced by a C-style cast there will be no overhead. If it can't, as in the case of dynamic_cast, for which there is no C equivalent, you have to pay the cost one way or another.

As an example, the following code:

int x;
float f = 123.456;

x = (int) f;
x = static_cast<int>(f);

generates identical code for both casts with VC++ - code is:

00401041   fld         dword ptr [ebp-8]
00401044 call __ftol (0040110c)
00401049 mov dword ptr [ebp-4],eax

The only C++ cast that can throw is dynamic_cast when casting to a reference. To avoid this, cast to a pointer, which will return 0 if the cast fails.

Is a C-style cast identical to a function-style cast?

I don't have a quote from the standard, but cppreference is usually good enough.

Explicit type conversion

The functional cast expression consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: unsigned int(expression) or int*(expression) are not valid), followed by a single expression in parentheses. This cast expression is exactly equivalent to the corresponding C-style cast expression.

As for Resharper, it's possible that to it C++ cast includes a functional cast, as that is only valid in C++.

The answer you linked in your question explains how safe a functional cast is. In your case int(d) should be equivalent to static_cast<int>(d). But in general a C-style or functional cast are unsafe as they can be equivalent to reinterpret_cast in certain situations, e.g. (double*)some_int_ptr.



Related Topics



Leave a reply



Submit