16 ms·
A list of modern C++ features
- mockery 10y agoNice summary! As someone who writes multi-platform C++, it would would be even greater (but a lot more work) if each feature listed which version of each major compiler introduced support. (ie. Can I use this feature if my codebase is currently compiling under given versions of Clang, GCC, VS.)
- Karliss 10y agoLess details about library but otherwise good compiler compatibility table can be found on cppreference http://en.cppreference.com/w/cpp/compiler_support http://en.cppreference.com/w/cpp/compiler_support .
- Too 10y agoIs there a similar list for linters, analyzers and editors? I've been in the situation many times where the compiler knows about a feature but the auto complete doesn't and the static analyzer will totally fail early and not report anything else except syntax error.
- slacka 10y agoGood supplementary material to the C++ Core Guidelines.[1] If you haven't checked it out yet, both VS 2015 and clang offer checkers[2] [1] https://github.com/isocpp/CppCoreGuidelines https://github.com/isocpp/CppCoreGuidelines [2] https://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ https://reviews.llvm.org/diffusion/L/browse/clang-tools-extr...
- codebeaker 10y agoI really wish there would be a linter (I realise it's probably a nearly impossible task) which would complain when you use legacy, unsafe language features. As someone who doesn't work in C++ or have the benefit of 10 years experience to see the flaws, trying to write modern, safe C++ is essentially impossible. I spent a solid week trying to write something safe and only use C++14/17 features when they were available, and encountered a mountain of outdated- and mis-information about what one can and can't do, what is and isn't considered safe and why one certain feature is better than another. It's nobody's fault that this happens, but with C++14 apparently fast becoming a language that is addressing it's #1 pitfall (safety) in an apparently very adequate way, it's frustrating that there's no "safe code" linter to stop rookies stepping on landmines. Thanks for the list, it's a solid start, and the sibling comment about C++ Core Guidelines is another solid resource.
- oblio 10y agoOr even better, at some point they should introduce "safe mode" or "strict mode", where you turn on a compiler flag.
- cygx 10y agoaka -Weverything ;)
- tomsmeding 10y agoWhich is hella annoying. -Wall -Wextra is very needed, -Wconversion is nice but already borderline annoying, but enabling everything would just be a mess.
- lorenzhs 10y ago-Wc++98-compat is especially annoying.
- uabstraction 10y agoYou guys don't use -pedantic? :D
- lorenzhs 10y ago-Weverything is even more pedantic than -pedantic: http://clang.llvm.org/docs/UsersManual.html#diagnostics-enable-everything http://clang.llvm.org/docs/UsersManual.html#diagnostics-enab... - pedantic can be a bit annoying, but Weverything is downright infuriating on a modern codebase
- cygx 10y agoThat's why you combine it with -Wno-... as appropriate on a per-project as well as per-file basis in addition to a simple way to disable warnings completely. When using make, it could look something like this: CC := clang CFLAGS := -std=c99 WFLAGS := -Weverything -Werror NOWARN := vla COMPILE = $(CC) -c $(CFLAGS) $(WFLAGS) $(NOWARN:%=-Wno-%) -o $@ $< foo.o: NOWARN += padded foo.o bar.o: %.o: %.c $(COMPILE) Building without warnings would be achieved via make WFLAGS=""
- augustk 10y agoThis made me think of the passage below from Edsger Dijkstra's ACM Turing Lecture from 1972. Back then there was no C++. "I remember from a symposium on higher level programming language a lecture given in defense of PL/1 by a man who described himself as one of its devoted users. But within a one-hour lecture in praise of PL/1. he managed to ask for the addition of about fifty new “features”, little supposing that the main source of his problems could very well be that it contained already far too many “features”. The speaker displayed all the depressing symptoms of addiction, reduced as he was to the state of mental stagnation in which he could only ask for more, more, more..." https://www.cs.utexas.edu/~EWD/transcriptions/EWD03xx/EWD340.html https://www.cs.utexas.edu/~EWD/transcriptions/EWD03xx/EWD340...
- pjmlp 10y agoAnd every time there was a language designed against PL/I like languages, with the goal of fighting complexity, its adoption by the industry lead to feature growth and becoming just as complex, usually with solutions hindering by backwards compatibility. Yes C++ is complex, but so are Python, Ruby, Ada, Fortran, C#, F#, Haskell, OCaml, Java... Even Go will get caught in the complexity game if it gets enterprise adoption at scale. If not the language, the libraries surely will.
- 0xAA55 10y agoThe big difference here is how additions in C++ tend to work - e.g. keeping all of the deprecated ill-defined legacy and growning features on it like tumors until there are no more symbols left. Unlike Ada and Python which aren't afraid of throwing away poor design and were overall more thought out in the first place (especially Ada). Its not a "complexity game" in so much as poor design being over-compensated for. C++ is like cancer infecting the minds of young programmers with buggy habits -- and no amount of features and static analysis will change what lies beneath... :p
- pjmlp 10y agoHow specially Ada?! The only thing Ada has thrown away was GC support that no compiler vendor has ever bothered to implement. What other features were thrown away across Ada83, Ada95, Ada2005 and Ada2012? If I present you Ada random code snippet X, are you sure you will be able to say which language version it requires? Also Python is the poster child of what happens when you throw compatibility away.
- wmu 10y agoIt is worth to note that template-related features make compilation time significantly longer. It might seem no-problem unless you face it in a bigger team, then these extra seconds (or minutes) are multiplied.
- mschuetz 10y agoC++11 and C++14 made me start to like C++. The build workflow is still horrible and cumbersome but at least the language itself has become nice to use.
- sprash 10y agoYeah more features is exactly what C++ needed... Am I the only one who uses C++ as C with classes? Sometimes I use vectors or strings and I like default values in functions and other minor improvements to C. I don't want to reimplement the n'th version of string concatenation when I can just use the "+" operator, sure... and there starts the rabbit hole. Before you know it you have a "protected abstract virtual base pure virtual private destructor" and have to explain the new hired mathematician who only has experience in R what the fuck you are doing.
- dom0 10y agoIn a word: C++ is probably not for you(r application).
- AnimalMuppet 10y agoDisagree. C++ has the philosophy of "You don't pay for what you don't use". Just use the parts that help with your problem. If your problem grows and you need more, you aren't trapped - the language has more. But if you don't need it, don't use it. For that matter, I'd argue that this is a reasonable approach for every language.
- rkv 10y agoI completely disagree. C++ was not created to extend or enhance C; its an entirely new language providing backwards compatability only for legacy reasons. Using C in C++ introduces deficiencies and dangers that C++ was designed to overcome. As a C++ developer I come across a lot of code where people still use things like FILE* and close the handle in a goto. That should be frowned upon, there is no advantage for this method vs. using a stream; which handles the file handle while being scope bounded. Promoting the use of C in C++ is something terrible misguided and plain wrong.
- stinos 10y agoAm I the only one who uses C++ as C with classes? I'd say unfortunately not. Focussing on the classes has the disadvantge it can quickly lead to 'everything is an object' or derived strict OOP mindsets thereby not only ignoring tons of other, more functional, goodies modern c++ has to offer but also leading to what you call 'protected abstract virtual base pure virtual private destructor'.
- Const-me 10y agoMost features are very good. My favorite ones are range-based loops, lambdas, and strongly-typed enums. The things I dislike most are pairs and tuples. They make the code enormously harder to read, understand and debug, compared to non-standard classes or structures with meaningful member names. Even this guide promotes them in “using Coordinate = std::pair<int, int>;” Please, never do that, create your own class with x/y or latitude/longitude instead.
- jstimpfle 10y agoSometimes all the meaning is in the name of a thing (or in the name of a function returning such a thing) and member names dont help but hurt. x/y is a good example, why not a/b? I prefer not having to make these decisions. Also tuple assignment syntax is very nice from a usability standpoint (I don't think C++ has it, but e.g. Python has).
- lorenzhs 10y agoC++17 adds tuple unpacking under the name "structured bindings" auto [a, b] = function_returning_pair(); Of course, tuple unpacking was already possible before if the variables were already declared: int a, b; std::tie(a, b) = function_returning_pair();
- Const-me 10y agoMaybe sometimes, but not this time. The author named their pair "Coordinate". In math and in computer graphics there’s widely known convention the fields of Cartesian coordinates are named x/y and not a/b. When you’ll start writing code that use and/or modify those Coordinate values, meaningful names in C++ source and in the debugger help a lot.
- lorenzhs 10y agoSometimes "using Coordinate = std::pair<int, int>;" does have upsides, though. One example would be when using a serialisation library that knows how to handle pairs. In that case you don't have to add (de-)serialisation code to every new class. It's also nice to be able to use std::tie for unpacking: "std::tie(x, y) = get_coords(blah);" If you use the members a lot, a struct with named members really is much easier to read, though.
- jonathanstrange 10y agoIt's a monster language, the Godzilla of the programming languages. But it gets the job done, as long as you use only 10% of its features.
- deleted 10y ago[deleted]
- digler999 10y agoAgreed. The template stuff blows me away, feels completely foreign to me. I'm barely catching up to C++11's features and looking at the '14/'17 stuff gives me that feeling of being assigned a ton of homework.
- lotu 10y agoThe one feature that really annoys me is the std::optional. This is awesome and very needed feature however it is too clunky for something that should be used very frequently. Swift does such a nice job with its '?' operator, I wish C++ had something similar.
- beojan 10y agoC++ tries to put features in the standard library instead of the core language whenever possible. The downside is they can't add new syntax.
- deleted 10y ago[deleted]
- reacharavindh 10y agoI really wish a group of really smart dev get together and come up with a new language based just on the modern C++. Perhaps even call it MC++ (Modern C++). Make it's syntax as simple and easy to learn as Python and yet give us the performance close to the compiled C++. I'd take that knowing very well that I can't do all the powerful stuff I could with C, yet I have a safe and idiot-proof language to work with in my hands.
- gregorburger 10y agoRust?
- reacharavindh 10y agoA very heavily biased rookie response. Not trying to be snarky. Rust would be an obvious choice for what I described. It's been on my personal list for a while to learn Rust and make use of it. I think I will pretty soon. I've been demotivated (to start) by reading over here on HN or elsewhere about how much of a learning curve it is to learn Rust. Perhaps, it was the bias that I already know what C++ was like in terms of syntax and idioms that made me ask for a choice with least friction.
- leshow 10y agoYou know c++ but you're worried about the complexity of Rust? I'm mostly a front-end software engineer and I got along just fine with Rust.
- pcwalton 10y ago> I've been demotivated (to start) by reading over here on HN or elsewhere about how much of a learning curve it is to learn Rust. That learning curve is necessary for safety. If you want a safe C++-like language without Rust's learning curve, you either (1) have a brilliant solution nobody has come up with yet or (2) don't actually want a safe language after all.
- piotrjurkiewicz 10y ago