Partners: KDAB and Whole Tomato Software

06 February 2017

How To Stay Sane with Modern C++

Complex C++

Have you seen my recent blog post with the list of C++17 features? Probably this is not the best measurement, but it got only ~30% of the average read (while other articles might get like 70, or even 90%). I am not complaining, the list is crazy and contains probably too many details of the new standard.

Another stat: C++ standard page count: from 879 pages for C++98/03 to 1586 for C++17 (draft)!

Do you need to learn all of that stuff to write good code?
How to stay sane in the C++ world today?

Intro

You probably know that C++ is a complex language. As I’ve found, there’s even a whole Wiki page about the criticism of Cpp. Modern C++ adds even more stuff to the package!

Here’s the full data about the page count in the specs that I’ve mentioned before:

Page count of C++ specs

It looks like C++17 is almost ~80% ‘larger’ than C++98/03. You can complain about added complexity and that it’s hard to learn all of those things. But is this so terrible? What can you do about the whole situation?

This post was motivated by some stories recently found::

First, let’s see some problems that you might bump into in C++.

Some Problems

Just to name a few:

Too slow pace

In 2016, as I wrote in my summary post, we got the draft for C++17. While it’s great, that we get a new standard every three years, a lot of developers complained that the new standard is not what everyone waited for.

A lot of features: like concepts, modules, ranges, co-routines, … were not accepted and we need to wait at least three more years to get them in the spec.

So, for some features, the pace of standardization is very slow.

As a positive aspect, I can only say that most of the mentioned features are already implemented, at least in the experimental form. So you can play with them and be prepared for the final version. Take a look at GCC concepts, modules in VS or Clang, Ranges, etc.

Too fast pace

As usually, we might have two contradicting opinions here. Although for some people the pace is slow, for others it’s hard to keep up with the changes.

You’ve just learned C++11… and now you need to update the knowledge with C++14, and then C++17 is along the way. Three years is not that short time, but bear in mind the compiler conformance, company policies, team guidelines might walk at a different pace.

Do your companies update to the most modern C++ version immediately?

Confusion / Complexity

Just read that comment:

CallMeDonk

I love c++. It’s my go to language, but you have to admit its ‘hodge podge’ implementation of value types is bizarre at best. Most programmers including me prefer simple well-defined language constructs over bizarre and over complicated grammar. I’m not a computer language lawyer. I’m a programmer.

Is C++ clear in every part? Probably not…

Move semantics

The principle of move semantics are quite clear: instead of copying just try to exchange the pointers to allocated memory, and you should get a nice performance boost. But the devil is in the detail.

I don’t write a lot of generic code, so fortunately I don’t have to think about move semantics all the time. But I was quite confused when I bumped into move and const - see my last article on that. I don’t believe every C++ will understand the rules here. Especially that you now need to remember about 6 default operations generated by the compiler: default constructor, destructor, copy constructor, move constructor, assign operator and move assignment operator.

Rvalues/xvalues/prvalues… myValues, fooValues

The last ones are made up… but still having all of the value categories is overwhelming!

Previously you just had to know lvalue vs rvalue, now it’s a bit more subtle.

Still, the question is if you need to know it by hard on a daily basis?

Some good comments:

c0r3ntin

It is complicated, but not on a daily basis.
Can this value be addressed ? can it be copied ? can it be moved ? Should it be moved ?
There is very few situation where you want to actively to be very specific and need a full understanding. ( templated library writing, hot paths, etc).
Most of the time C++ is not more complicated than java or something. Sadly this is lost on most people. C++ may be the most complex language out there but you can write very good code without caring about the specific.
BigObject o = getBigObject();

Initialization

18 ways now! - Initialization in C++ is bonkers and the r/cpp thread

Template deduction

I was quite lost when I saw all the changes for C++17; there are so many details about templates deduction!

Although you might not write template code all the time (unless you’re a library developer), it’s good to remember that deduction rules now also apply on ‘auto’ type deduction since we have AAA (Almost Always Auto) rule.

Fortunately, the rules are getting a bit easier with C++17 where there are things like template <auto>, fixing auto i { 0 }, template template parameters, etc…

Others areas?

What are your main problems with the language?

So far, we’ve discussed some problems, so how to live with them… and possibly how to solve them?

How to stay sane

There’s no the perfect programming language, every one of them has some issues. Here are my suggestions how to cope with the problems of Modern C++.

Stay positive, the language is evolving

No one wants to write code using old tools. We’ve already seen a lot of complaints about old C++ before C++11. It took almost 13 years (counting from major C++98, not including minor C++03) to came up with the major version: C++11. Now we can be happy that we get back on track, and every three years there will be some changes. At the end of the day, you cannot say that your language is dead and old.

And the tools as well!

Thanks to Clang and also improved development speed in other platforms, we get tools like:

While it’s not super great as for other languages (especially Java based or .NET based), it’s getting better and better. Bear in mind that because of the complex C++ grammar it’s very hard to implement tools that analyse the code on the fly.

Try to stay up to date

C++ community is very much alive. There are many blogs, books, conferences… and there’s even a chance a local community is in your city!

For a start, I just suggest going to isocpp.org the central place for all of the events/news/articles. Then you might check Meeting C++ and info about local C++ groups. There’s also reddit/cpp where constantly more and more good stuff is posted.

And remember about books like:

You can also take a look at my recent C++17 Lang Ref Card!

Too much details? Just don’t open the hood

One of the reasons C++ has so much power is that it allows you to implement code very close to the metal. You have control over all of the details, memory layout, performance optimizations, etc, etc… At the same time, such abilities increase the complexity of the language.

Still, if you don’t need to go that far, you can stay at a relatively higher level of abstraction.

Use what you need

C++ is a multi-paradigm language; you can use it many different ways. Recently, I’ve read an interesting comment that said that a Cpp programmer might for years do very well without touching advanced stuff like template metaprogramming or even exceptions. This heavily depends on the code style of the project.

Even such companies like Google limit features of C++, for example, they don’t use exceptions.

This is a bit of repetition, but if you’re not a library developer you might not get into troubles with custom move operators or move constructors. Similarly, advances metaprogramming stuff might also not be a crucial part of your code.

Incremental change

If you start from scratch or have a small code base then going to C++11/14 should be relatively easy. What about million-line of code, code that was started 20 years (or more!) ago?

Just do it step by step.

At least for the new code, you should start using Modern C++. Moreover, by applying “The Boy Scout Rule” you can improve surrounding code that you touch.

This will probably result in some mixed code, but still, it’s better than staying with the legacy style only.

Last resort: your old code will still compile!

One of the reason why C++ specs are getting larger and larger is that the language is backward compatible. So the committee usually introduces new features, but rarely remove the old stuff. So… your code can still compile. If you don’t want to move and use newer things then you can stay with your current style.

From time to time you’ll get some warnings about deprecated stuff or removed features (like auto_ptr in C++17), but even in that case, you can switch the compiler to target some older C++ standard.

Summary

This article is partially a rant, partially a ‘glorification.’ I try to see the bad sides of the language and its evolution process and some positive signs as well.

While we can complain about the complexity, pace of changes, etc… I think we cannot say that the language is dead. That’s a good thing.

I don’t think you have to rapidly chase the new features and immediately rewrite your existing code. Just try to stay up to date with news, use the features that really improves your work and gradually your code should improve and be more ‘modern’ (however, can that be defined - see meetingcpp article on that).

  • What’s your approach when adopting new features from C++11/14/17/1z ?
  • What’s your main problem with C++?
  • Do you use modern C++ in your work?

Interested in new blog posts and bonus content? Sign up for my newsletter.

© 2017, Bartlomiej Filipek, Blogger platform
Any opinions expressed herein are in no way representative of those of my employers.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.