Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to use C-style cast for built-in types?

Tags:

c++

casting

After reading here a lot of answers about C-style casting in C++ I still have one little question. Can I use C-style casting for built-in types like long x=(long)y; or it's still considered bad and dangerous?

like image 375
jackhab Avatar asked Feb 09 '09 12:02

jackhab


People also ask

Why is static_cast better than C-style cast?

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

What does C-style cast do?

C-style casts can be used to convert any type into any other type, potentially with unsafe results (such as casting an integer into a pointer type).

Can I use static_cast in C?

Static casts are only available in C++.

Why should you use the C++ type casting features static_cast Const_cast etc rather than the old school C variety?

It helps maintain correctness, since static_cast cannot do some casts which a C-style cast can.


2 Answers

Can I use C-style casting for built-in types like long x=(long)y; or it's still considered bad and dangerous?

Don't use them, ever. The reasons against using them applies here as well. Basically, once you use them, all bets are off because the compiler won't help you any more. While this is more dangerous for pointers than for other types, it's potentially still dangerous and gives poor compiler diagnostics in the case of errors, whereas new style casts offer richer error messages since their usage is more constrained: Meyers cites the example of casting away constness: using any cast other than const_cast won't compile, thus making it clear what happens here.

Also, some other disadvantages apply regardless of the types, namely syntactic considerations: A C-style cast is very unobtrusive. This isn't good: C++ casts stand out clearly in the code and point to potentially dangerous code. They can also easily be searched for in IDEs and text editors. Try searching for a C-style cast in a large code and you'll see how hard this is.

On the other hand, C-style casts offer no advantages over C++ casts so there's not even a trade-off to consider.

More generally, Scott Meyers advises to “Minimize casts” in Effective C++ (item 27), because “casts subvert the type system.”

like image 101
Konrad Rudolph Avatar answered Oct 11 '22 01:10

Konrad Rudolph


I would not, for the following reasons:

  • Casts are ugly and should be ugly and stand out in your code, and be findable using grep and similar tools.
  • "Always use C++ casts" is a simple rule that is much more likely to be remembered and followed than, "Use C++ casts on user-defined types, but it's OK to use C-style casts on built-in types."
  • C++ style casts provide more information to other developers about why the cast is necessary.
  • C-style casts may let you do conversions you didn't intend -- if you have an interface that takes in (int*) and you were using c-style casts to pass it a const int*, and the interface changes to take in a long*, your code using c-style casts will continue to work, even if it's not what you wanted.
like image 42
JohnMcG Avatar answered Oct 11 '22 02:10

JohnMcG