7 ms·
ISO C++11 Published
- andrewcooke 15y agoso is there a good book that shows how to write c++ code correctly, so that it's as "clean and safe as code written other modern languages"? for example, it would probably avoid introducing pointers until very late, using standard containers for data structures, etc. the approach would be more "here is a modern language using the right subset of c++" rather than "you can make clunky old c++ work by following these arcane rules to avoid weird exceptions" (ie it shows a complete, modern language, not a collection of work-arounds). and preferably it should be aimed at an experienced programmer, so it should be compact and not try teach me "how to program" in general.
- paulkoer 15y agoI think that "Accelerated C++ Programming by Example" [1] is a good introduction to modern C++. It focuses on the correct use of the standard template library which I think is at the heart of much of modern C++. Needless to say, C++ is very complex and so the book doesn't cover a good part of it. But I think it is a fairly good start and is not too long either. [1] http://www.acceleratedcpp.com/ http://www.acceleratedcpp.com/
- andrewcooke 15y agothanks, but although they look like good authors, and it's promisingly slim, it's also worryingly old. is there nothing more modern? will it include things like boost's shared_ptr (see comment above)?
- tbrownaw 15y ago> probably avoid introducing pointers until very late Introduce shared_ptr<> early, explain how actual pointers should pretty much only be used for compatibility with old-style libraries or if you're the one writing the standard library.
- exDM69 15y agoI would say that using a shared_ptr is going the wrong way. A shared_ptr is a reference counter pointer and as such as many shortcomings, such as not being able to do cyclic data structures, being slow, hard to make thread safe. They're also quite complicated and require knowledge of C++ destructors, copycons, RAII, etc to understand. So, please, don't push shared_ptr as a magic one-size-fits all solution, especially to newbies. There's a reason why reference counting is not used everywhere. With C++ you must remember that it's using pass-by-value semantics by default, which is very different from pass-by-reference semantics of other (syntactically similar) languages (like Java). Using shared_ptr's will not get rid of this fact and makes things even more complicated. In fact, I have a very hard time to find _any_ use for shared_ptr's in my C++ projects. scoped_ptr's and ptr_containers have proven to be more useful.
- mathias_10gen 15y agoActually since most if not all CPUs have an atomic increment instruction it is quite easy to make shared_ptr<> thread-safe. In fact, that is what boost::shared_ptr<> does. This makes it a good tool for resources that may be shared between threads or for cases where it's just not worth the mental overhead of worrying about ownership. That said, shared_ptr isn't a solution to everything. Sometimes scoped_ptr (called unique_ptr in tr1) or intrusive_ptr is a better tool. Often if you don't want to share or transfer ownership, passing around a raw ptr or reference is even better and the only option if you want to work with embedded structs (but the newed memory should still be owned by some RAII smart pointer even if it gets passed around as a bare pointer)
- apaprocki 15y agoWhile there are atomic instructions, they usually require memory barriers to ensure modern CPUs order the instructions properly to get the desired behavior. This can have a negative effect on overall performance if these atomic ops are hit in hot spot.
- cageface 15y agoUnderstanding constructors, destructors and RAII is a prerequisite for any real C++ programming. Avoiding shared_ptr for performance reasons before you know it's a bottleneck (it probably isn't) is a premature optimization.
- mathias_10gen 15y agoI think Effective C++ by Scott Meyers[1] is the gold standard when it comes to books on writing good clean C++. It assumes that you already know the basic syntax though and already know how to program. This allow him to skip the intro stuff that take up the most space in many books and get strait to telling you what you need to know to write better code. [1] http://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876 http://www.amazon.com/Effective-Specific-Improve-Programs-De... (Make sure you get the latest edition, a lot has changed for the better)
- cageface 15y agoMeyers book has been the book I'd recommend to anybody moving beyond the beginner stage in C++, but there's so much critical stuff in the new standard that the last edition of his book doesn't cover that I'd hesitate to recommend it now. I think you're better off studying some of the resources available online, like the videos at http://channel9.msdn.com/ http://channel9.msdn.com/ That is, of course, only until a new edition of Meyers' book comes out.
- tdicola 15y agoThis is a good intro to modern C++ from Herb Sutter: http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-835T http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-835T It's from MS's Build conference but not specific to any platform or tools. I really enjoyed the video though, especially when he compares an old C-style program to a modern C++ version--very much an aha! moment for me that C++ has evolved into a really nice language.
- aklein 15y agoThe final publication costs about $400 USD. Here is a working draft: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n324...
- lallysingh 15y agoWell, we'll see. The C++9x was $400 printed, but $18 in PDF form.
- pmr_ 15y agoThe C++ standard comes at an impressive page count of 1300+. The print version seems entirely useless to me compared to a searchable PDF with links and chapters. Why on earth would you get it?
- norswap 15y agoTo me, it is mind-boggling they won't release it online for free.
- tomjen3 15y agoI wonder if it would be of any use. How much support is there for C++11 in compilers today?
- mathias_10gen 15y agoSurprisingly good although not complete: http://gcc.gnu.org/projects/cxx0x.html http://gcc.gnu.org/projects/cxx0x.html Unfortunately a lot of companies don't use today's compilers, but use many-year old versions. For example, Apple still ships gcc 4.2 which is over 4 years old now and supports none of these features.
- tbe 15y agoThe reason why Apple and their ilk use an old version of GCC, is that newer versions are licensed under GPLv3 which does not work for them. For the same reason they are switching to LLVM. So if Clang supports C++11 well enough, Apple switches to that, and others who wont use newer GCC versions do the same, many companies may be back on track regarding compiler compatibility.