views:

2130

answers:

8

A question related to Regular cast vs. static_cast vs. dynamic_cast:

What cast syntax style do you prefer in C++?

  • C-style cast syntax: (int)foo
  • C++-style cast syntax: static_cast<int>(foo)
  • constructor syntax: int(foo)

They may not translate to exactly the same instructions (do they?) but their effect should be the same (right?).

If you're just casting between the built-in numeric types, I find C++-style cast syntax too verbose. As a former Java coder I tend to use C-style cast syntax instead, but my local C++ guru insists on using constructor syntax.

What do you think?

+1  A: 

C-style cast syntax, do not error check. C++-style cast syntax, does some checking. When using static_cast, even if it doesn't do checking, at least you know you should be carefull here.

CiNN
+3  A: 

According to Stroustrup:

The "new-style casts" were introduced to give programmers a chance to state their intentions more clearly and for the compiler to catch more errors.

So really, its for safety as it does extra compile-time checking.

hometoast
A: 

C-style cast is the worst way to go. It's harder to see, ungreppable, conflates different actions that should not be conflated, and can't do everything that C++-style casts can do. They really should have removed C-style casts from the language.

DrPizza
+18  A: 

It's best practice never to use C-style casts for two main reasons:

  • as already mentioned, no checking is performed here. The programmer simply cannot know which of the various casts is used which weakens strong typing
  • the new casts are intentionally visually striking. Since casts often reveal a weakness in the code, it's argued that making casts visible in the code is a good thing.
  • this is especially true if searching for casts with an automated tool. Finding C-style casts reliably is nearly impossible.

As palm3D noted:

I find C++-style cast syntax too verbose.

This is intentional, for the reasons given above.

The constructor syntax (official name: function-style cast) is semantically the same as the C-style cast and should be avoided as well (except for variable initializations on declaration), for the same reasons. It is debatable whether this should be true even for types that define custom constructors but in Effective C++, Meyers argues that even in those cases you should refrain from using them. To illustrate:

void f(auto_ptr<int> x);

f(static_cast<auto_ptr<int> >(new int(5))); // GOOD
f(auto_ptr<int>(new int(5));                // BAD

The static_cast here will actually call the auto_ptr constructor.

Konrad Rudolph
+1  A: 

I use static_cast for two reasons.

  1. It's explicitly clear what's taking place. I can't read over that without realizing there's a cast going on. With C-style casts you eye can pass right over it without pause.
  2. It's easy to search for every place in my code where I'm casting.
Bill the Lizard
+1  A: 

Definitely C++-style. The extra typing will help prevent you from casting when you shouldn't :-)

Ben Collins
+2  A: 

Regarding this subject, I'm following the recommandations made by Scott Meyers (More Effective C++, Item 2 : Prefer C++-style casts).

I agree that C++ style cast are verbose, but that's what I like about them : they are very easy to spot, and they make the code easier to read (which is more important than writing).

They also force you to think about what kind of cast you need, and to chose the right one, reducing the risk of mistakes. They will also help you detecting errors at compile time instead at runtime.

Jérôme
+1  A: 

We currently use C-style casts everywhere. I asked the other casting question, and I now see the advantage of using static_cast instead, if for no other reason than it's "greppable" (I like that term). I will probably start using that.

I don't like the C++ style; it looks too much like a function call.

Graeme Perrow
looking like a function call can be nice, it allows you to have utility functions which share the same style such as the common `lexical_cast` for converting from strings <-> numeric types. But that's just an opinion.
Evan Teran