What is the difference between static_cast<> and C style casting?

C++CastingStatic Cast

C++ Problem Overview


Is there any reason to prefer static_cast<> over C style casting? Are they equivalent? Is there any sort of speed difference?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

In short:

  1. static_cast<>() gives you a compile time checking ability, C-Style cast doesn't.
  2. static_cast<>() is more readable and can be spotted easily anywhere inside a C++ source code, C_Style cast is'nt.
  3. Intentions are conveyed much better using C++ casts.

More Explanation:

The static cast performs conversions between compatible types. It is similar to the C-style cast, but is more restrictive. For example, the C-style cast would allow an integer pointer to point to a char.

char c = 10;       // 1 byte
int *p = (int*)&c; // 4 bytes

Since this results in a 4-byte pointer ( a pointer to 4-byte datatype) pointing to 1 byte of allocated memory, writing to this pointer will either cause a run-time error or will overwrite some adjacent memory.

*p = 5; // run-time error: stack corruption

In contrast to the C-style cast, the static cast will allow the compiler to check that the pointer and pointee data types are compatible, which allows the programmer to catch this incorrect pointer assignment during compilation.

int *q = static_cast<int*>(&c); // compile-time error

You can also check this page on more explanation on C++ casts : Click Here

Solution 3 - C++

See A comparison of the C++ casting operators.

> However, using the same syntax for a variety of different casting operations can make the intent of the programmer unclear. > >Furthermore, it can be difficult to find a specific type of cast in a large codebase. > > the generality of the C-style cast can be overkill for situations where all that is needed is a simple conversion. The ability to select between several different casting operators of differing degrees of power can prevent programmers from inadvertently casting to an incorrect type.

Solution 4 - C++

struct A {};
struct B : A {};
struct C {}; 

int main()
{
    A* a = new A;    
    
    int i = 10;
    
    a = (A*) (&i); // NO ERROR! FAIL!
    
    //a = static_cast<A*>(&i); ERROR! SMART!
    
    A* b = new B;
    
    B* b2 = static_cast<B*>(b); // NO ERROR! SMART!
    
    C* c = (C*)(b); // NO ERROR! FAIL!
    
    //C* c = static_cast<C*>(b); ERROR! SMART!
}

Solution 5 - C++

A great post explaining different casts in C/C++, and what C-style cast really does: https://anteru.net/blog/2007/12/18/200/index.html

> C-Style casting, using the (type)variable syntax. The worst ever > invented. This tries to do the following casts, in this order: (see > also C++ Standard, 5.4 expr.cast paragraph 5) > > 1. const_cast > 2. static_cast > 3. static_cast followed by const_cast > 4. reinterpret_cast > 5. reinterpret_castfollowed by const_cast

Solution 6 - C++

static_cast checks at compile time that conversion is not between obviously incompatible types. Contrary to dynamic_cast, no check for types compatibility is done at run time. Also, static_cast conversion is not necessarily safe.

static_cast is used to convert from pointer to base class to pointer to derived class, or between native types, such as enum to int or float to int.

The user of static_cast must make sure that the conversion is safe.

The C-style cast does not perform any check, either at compile or at run time.

Solution 7 - C++

Since there are many different kinds of casting each with different semantics, static_cast<> allows you to say "I'm doing a legal conversion from one type to another" like from int to double. A plain C-style cast can mean a lot of things. Are you up/down casting? Are you reinterpreting a pointer?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondicroceView Question on Stackoverflow
Solution 1 - C++GlenView Answer on Stackoverflow
Solution 2 - C++HosseinView Answer on Stackoverflow
Solution 3 - C++Eugene YokotaView Answer on Stackoverflow
Solution 4 - C++Rishi KhanejaView Answer on Stackoverflow
Solution 5 - C++Ying XiongView Answer on Stackoverflow
Solution 6 - C++kiriloffView Answer on Stackoverflow
Solution 7 - C++Doug T.View Answer on Stackoverflow