TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Elements of Modern C++ Style

148 pointsby rsaarelmover 13 years ago

11 comments

obiterdictumover 13 years ago
We have just started using some C++11 features, and with new features come new traps and potential complexities, as you'd expect with C++.<p>A few examples:<p>1. Implicit lambdas ("[=]") capture "this" pointer for data members, if you capture a shared pointer and let the lambda object leave the scope, the shared pointer won't be copied (<a href="http://stackoverflow.com/q/7764564/23643" rel="nofollow">http://stackoverflow.com/q/7764564/23643</a>), which may create unpleasant results, like code blowing up in your face.<p>2. Overuse of "auto" (not exclusive to C++) may make the code harder to read, especially if storing a result returned from a function. You can't tell if the return value is a smart pointer or a raw pointer, value or a reference, so you can't figure ownership semantics just by looking at the local code.<p>3. Move constructors/assignment operators are a bit of a black magic, especially with regard to suppressing default implementation (<a href="http://mmocny.wordpress.com/2010/12/09/implicit-move-wont-go" rel="nofollow">http://mmocny.wordpress.com/2010/12/09/implicit-move-wont-go</a>) and exception safety (<a href="http://cpp-next.com/archive/2009/10/exceptionally-moving" rel="nofollow">http://cpp-next.com/archive/2009/10/exceptionally-moving</a>). I would refrain from using it, until I fully understand it.<p>With that said, most of C++11 is a welcome change and makes C++ more pleasant to write, but, as always, you have to have your guard up and use new features judiciously.
评论 #3172043 未加载
评论 #3172198 未加载
评论 #3172069 未加载
评论 #3172003 未加载
评论 #3172026 未加载
forrestthewoodsover 13 years ago
When I read articles like this I think of a quote from Carmack.<p>"The Escalation programmers come from a completely different background, and the codebase is all STL this, boost that, fill-up-the-property list, dispatch the event, and delegate that. I had been harboring some suspicions that our big codebases might benefit from the application of some more of the various “modern” C++ design patterns, despite seeing other large game codebases suffer under them. I have since recanted that suspicion." (<a href="http://www.bethblog.com/index.php/2010/10/29/john-carmack-discusses-rage-on-iphoneipadipod-touch/" rel="nofollow">http://www.bethblog.com/index.php/2010/10/29/john-carmack-di...</a>)<p>The rage in the game dev community these days is all about highly cache aware Data Oriented Design (DOD) over OOP. Curiously there aren't a lot of DOD discussions or articles on HN.
评论 #3172878 未加载
评论 #3172807 未加载
cagefaceover 13 years ago
I've been using a lot of these new features in XCode 4 and my code is already <i>much</i> cleaner and more readable. Modern C++ really is a pleasure to use and I love writing code that's this high level but still smoking fast.
评论 #3172305 未加载
评论 #3172020 未加载
pepsi_canover 13 years ago
This c++ is very different from the c++ I remember. I'd like to learn more about modern c++ style and idioms. Can anyone recommend a good text?
评论 #3172679 未加载
评论 #3172324 未加载
评论 #3172650 未加载
gcvover 13 years ago
That looks surprisingly good, and might even improve new projects which have to use C++. How long until compilers support all this?
评论 #3171987 未加载
pandamanover 13 years ago
Auto everywhere is pretty dangerous despite what Mr. Sutter says. Many C++ libraries rely on type conversion (in high performance math it's very common however you can find it as close as the STL's vector&#60;bool&#62;). Whenever you use your own memory allocator and/or care about performance (and this is whenever you are writing high performance/high reliability code) library smart pointers are pretty useless too. Other things are not as controversial as these two, but don't really define the modern C++. Herb should have read his friend's, Anderi Alexandrescu's, book from ten years ago to get a hint of what modern C++ looks like :) (I know he has read it. I guess he is just trying to pimp C++ to Java/PHP/Python/etc developers).
评论 #3172857 未加载
X-Istenceover 13 years ago
The only issue I have ran into is that some of these newer features are not yet available in compilers that are shipping with OS's we are deploying on, so I HAVE to continue writing to C++98 since installing a new compiler/libraries is a no go (if it is not vendor supported, can't do it, thank you US gov't rules!).<p>It will take some time for the compilers and their libc++ libraries to move forward and have older versions of OS's replaced by ones that do support it.<p>That being said, wherever I can (in personal projects) I will slowly start adapting and using the new features...
kleibaover 13 years ago
I don't know a lot of C++, but the examples that demonstrate how C++11 compares to C++98 look like a vast improvement. Until you come down to this monster:<p><pre><code> auto i = find_if( begin(v), end(v), [=](int i) { return i &#62; x &#38;&#38; i &#60; y; } ); </code></pre> This is the <i>improved</i> version by the way, but I'm not sure if a loop wouldn't be more readable for such a straight-forward problem. Plus I can't help but think that C++ will probably never win a beauty contest.
评论 #3173176 未加载
评论 #3174258 未加载
评论 #3173629 未加载
protomythover 13 years ago
Does anyone have a recommendation for an intro book for experienced programmers (not C++ programmers) that teaches C++ using a "good" C++11 style?
评论 #3172308 未加载
评论 #3173216 未加载
zurnover 13 years ago
Best joke is at the end: "These features [...] make modern C++ the clean, and safe, and fast language that our industry will continue relying on heavily for years to come."
ericHosickover 13 years ago
Will C++ get properties some day (similar to C# properties)?