<p>A question related to <a href="https://stackoverflow.com/questions/28002">Regular cast vs. static_cast vs. dynamic_cast</a>:</p>

<p>What cast syntax style do you prefer in C++?</p>

<ul>
<li>C-style cast syntax: <code>(int)foo</code></li>
<li>C++-style cast syntax: <code>static_cast&lt;int&gt;(foo)</code></li>
<li>constructor syntax: <code>int(foo)</code></li>
</ul>

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

<p>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.</p>

<p>What do you think?</p>

[{'answer_id': 32180, 'author': 'CiNN', 'author_id': 2566, 'author_profile': 'https://Stackoverflow.com/users/2566', 'pm_score': 1, 'selected': False, 'text': "<p>C-style cast syntax, do not error check.\nC++-style cast syntax, does some checking.\nWhen using static_cast, even if it doesn't do checking, at least you know you should be carefull here.</p>\n"}, {'answer_id': 32194, 'author': 'hometoast', 'author_id': 2009, 'author_profile': 'https://Stackoverflow.com/users/2009', 'pm_score': 4, 'selected': False, 'text': '<p>According to <a href="http://www.research.att.com/~bs/bs_faq2.html#static-cast" rel="noreferrer">Stroustrup</a>:</p>\n\n<blockquote>\n  <p>The "new-style casts" were introduced\n  to give programmers a chance to state\n  their intentions more clearly and for\n  the compiler to catch more errors.</p>\n</blockquote>\n\n<p>So really, its for safety as it does extra compile-time checking.</p>\n'}, {'answer_id': 32210, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 32224, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 7, 'selected': True, 'text': "<p>It's best practice <em>never</em> to use C-style casts for three main reasons:</p>\n\n<ul>\n<li>as already mentioned, no checking is performed here. The programmer simply cannot know which of the various casts is used which weakens strong typing</li>\n<li>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.</li>\n<li>this is especially true if searching for casts with an automated tool. Finding C-style casts reliably is nearly impossible.</li>\n</ul>\n\n<p>As palm3D noted:</p>\n\n<blockquote>\n  <p>I find C++-style cast syntax too verbose.</p>\n</blockquote>\n\n<p>This is intentional, for the reasons given above.</p>\n\n<p>The constructor syntax (official name: function-style cast) is semantically <em>the same</em> 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:</p>\n\n<pre><code>void f(auto_ptr&lt;int&gt; x);\n\nf(static_cast&lt;auto_ptr&lt;int&gt; &gt;(new int(5))); // GOOD\nf(auto_ptr&lt;int&gt;(new int(5));                // BAD\n</code></pre>\n\n<p>The <code>static_cast</code> here will actually call the <code>auto_ptr</code> constructor.</p>\n"}, {'answer_id': 32225, 'author': 'Bill the Lizard', 'author_id': 1288, 'author_profile': 'https://Stackoverflow.com/users/1288', 'pm_score': 2, 'selected': False, 'text': "<p>I use static_cast for two reasons.</p>\n\n<ol>\n<li>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.</li>\n<li>It's easy to search for every place in my code where I'm casting.</li>\n</ol>\n"}, {'answer_id': 32228, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 2, 'selected': False, 'text': "<p>Definitely C++-style.  The extra typing will help prevent you from casting when you shouldn't :-)</p>\n"}, {'answer_id': 32234, 'author': 'Jérôme', 'author_id': 2796, 'author_profile': 'https://Stackoverflow.com/users/2796', 'pm_score': 3, 'selected': False, 'text': '<p>Regarding this subject, I\'m following the recommandations made by <a href="http://en.wikipedia.org/wiki/Scott_Meyers" rel="noreferrer">Scott Meyers</a> (<a href="https://rads.stackoverflow.com/amzn/click/com/020163371X" rel="noreferrer" rel="nofollow noreferrer">More Effective C++</a>, Item 2 : Prefer C++-style casts).</p>\n\n<p>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).</p>\n\n<p>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.</p>\n'}, {'answer_id': 32267, 'author': 'Graeme Perrow', 'author_id': 1821, 'author_profile': 'https://Stackoverflow.com/users/1821', 'pm_score': 1, 'selected': False, 'text': '<p>We currently use C-style casts everywhere. I asked the other <a href="https://stackoverflow.com/questions/28002">casting question</a>, 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.</p>\n\n<p>I don\'t like the C++ style; it looks too much like a function call.</p>\n'}, {'answer_id': 9457254, 'author': 'CAH', 'author_id': 1234388, 'author_profile': 'https://Stackoverflow.com/users/1234388', 'pm_score': 1, 'selected': False, 'text': "<p>Go for C++ style and, at worse, the ugly verbose code snippets that comprised C++'s explicit typecast will be a constant reminder of what we all know (i.e explicit casting is bad -- the lead to the coin-ing of expletives). \nDo not go with C++ style if you want to master the art of tracking runtime errors. </p>\n"}, {'answer_id': 41303511, 'author': 'QBziZ', 'author_id': 11572, 'author_profile': 'https://Stackoverflow.com/users/11572', 'pm_score': 2, 'selected': False, 'text': "<p>The constructor syntax. C++ is OO, constructors exist, I use them. \nIf you feel the need to annotate these conversion ctor's you should do it for every type, not just the built-in ones. Maybe you use the 'explicit' keyword for conversion ctors but the client syntax mimics exactly what the ctor syntax for built-in types does.\nBeing greppable, that may be true, but what a big surprise that typing more characters makes searches easy. Why treat these ones as special? \nIf you are writing math formulas with lots of int/unsigned/... to and from double/float - graphics - and you need to write a static_cast every time, the look of the formula gets cluttered and is very much unreadable.\nAnd it's an uphill battle anyway as a lot of times you will convert without even noticing that you are. \nFor downcasting pointers I do use the static_cast as of course no ctor exists by default that would do that.</p>\n"}]
['https://Stackoverflow.com/questions/32168', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2686/']