
A list of modern C++ features - deepakkarki
https://github.com/AnthonyCalandra/modern-cpp-features
======
codebeaker
I 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.

~~~
adrianN
clang-tidy does this (or is working towards doing this)

[http://clang.llvm.org/extra/clang-
tidy/checks/list.html](http://clang.llvm.org/extra/clang-
tidy/checks/list.html)

~~~
codebeaker
Thanks, that page looks like hell, but I recognise a few terms.

[http://clang.llvm.org/extra/clang-tidy/](http://clang.llvm.org/extra/clang-
tidy/) is much more approachable, and gives a solid overview.

The introductory sentence gave me project-naming cancer:

> clang-tidy is a clang-based C++ “linter” tool.

Then why is it called _tidy_ ??? It appears first on page three of google's
search results for "C++ linter" in spite of the fact that anything from LLVM
is probably a) current and b) of exceptional quality.

A little marketing-mindset would go a long, long way for these kinds of
things.

Whilst the Ruby community lead by the example of DHH often takes this concept
too far, it is inarguably effective.

~~~
masklinn
> Then why is it called tidy ???

[https://en.wikipedia.org/wiki/HTML_Tidy](https://en.wikipedia.org/wiki/HTML_Tidy)

~~~
andystanton
It's also a (possibly unintentional) play on the British slang term "bang
tidy" [http://www.collinsdictionary.com/dictionary/english/bang-
tid...](http://www.collinsdictionary.com/dictionary/english/bang-tidy)

------
sprash
Yeah 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
In a word: C++ is probably not for you(r application).

~~~
AnimalMuppet
Disagree. 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
I 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.

------
augustk
This 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...](https://www.cs.utexas.edu/~EWD/transcriptions/EWD03xx/EWD340.html)

~~~
pjmlp
And 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
The 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
How 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.

~~~
0xAA55
What I meant to emphasize was that Ada was well designed in the first place,
and yes GC was a problem and so were some of the restrictions and aspects of
the standard libraries. For example, a big one was unconstrained types for
generic actuals...

C is a mine-field of devastating design flaws which have caused many people to
die because of decades old decisions made on a whim and any change to basic
libraries to make them safer and removing dangerous functions is heresy.

As far as Python, yes the two versions of the language is troublesome for
users but the language is objectively better and students learning programming
for the first time benefit from a more robust version.

Feel free to give me a random code snippet ;p

~~~
AnimalMuppet
> C is a mine-field of devastating design flaws which have caused many people
> to die because of decades old decisions made on a whim...

Wow. Hyperbole much?

I get that you don't like C, but you sound like a raving lunatic who is
completely unconnected with reality.

Devastating design flaws? Flaws I can agree with. Devastating? That sounds a
bit overstated.

Caused many people to die? I call BS. (Yes, I'm pretty sure you might be able
to document a few. But you said "many", and I'm calling you on it.)

Decisions made on a whim? Again, BS. In fact, at this point you're just making
stuff up.

~~~
0xAA55
First of all what if I am lunatic xD

There are so many parts of C that are unsafe that a secondary tool is needed
to have an reasonable type-checking and that tends to be devastating if they
can be easily avoided by even safety critical engineers.

[https://users.ece.cmu.edu/~koopman/pubs/koopman14_toyota_ua_...](https://users.ece.cmu.edu/~koopman/pubs/koopman14_toyota_ua_slides.pdf)
"Toyota does not claim to have followed MISRA Guidelines"

And honestly I dont think we agree on what "a few" is. In my book if any more
than 4 or 10 people die from a solved problem in computer science (like strong
typing, mutexes, and range checking) that is already way too _yes_ many

As far as decisions on a whim compare the two language design efforts and i'll
let you decide:

[http://www.adapower.com/index.php?Command=Class&ClassID=FAQ&...](http://www.adapower.com/index.php?Command=Class&ClassID=FAQ&CID=320)
"The requirements were not a language specification, but instead they
attempted to define rigorously the needed characteristics in a form that could
be critically reviewed."

[https://www.codingunit.com/the-history-of-the-c-
language](https://www.codingunit.com/the-history-of-the-c-language) “It’s
entirely Dennis Ritchie’s work”

~~~
AnimalMuppet
"Decided by one man" != "decided on a whim".

~~~
0xAA55
"...despite the changes that it has undergone since its first published
description, which was admittedly _informal and incomplete_..." ~ Dennis M.
Ritchie

It took a decade to even decide what the C language really was and standardize
it -- sounds pretty undefined and whimsical to me. But we all use and
understand words differently so let's agree to disagree :)

------
slacka
Good 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-
extr...](https://reviews.llvm.org/diffusion/L/browse/clang-tools-
extra/trunk/clang-tidy/cppcoreguidelines/)

------
mschuetz
C++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.

------
jonathanstrange
It'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.

~~~
digler999
Agreed. 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
The 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
C++ tries to put features in the standard library instead of the core language
whenever possible. The downside is they can't add new syntax.

------
reacharavindh
I 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
Rust?

~~~
reacharavindh
A 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.

~~~
pcwalton
> 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.

------
mockery
Nice 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
Less 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
Is 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.

------
Const-me
Most 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
Sometimes 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
C++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();

------
wmu
It 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.

