What does "Modern C++" really mean?
published at 13.10.2016 16:03 by Jens Weller
I submitted a talk to CppCon with Modern C++ in the title: CppCon 2016 - Jens Weller “Writing applications in modern C++ and Qt". It seemed a good title, and I think it still is. Yet it made me interested in the term "modern C++", as its used so often. And I have heard so many things what it is, and what it isn't.
So lets say, you're in a job interview, things going quite good. Last question: define what modern C++ is. This is a very mean question, as you now have no idea, if you better match the interviewers definition (is it an HR person, a C++ Dev or just a random manager?) or openly say what you think it is. Or something in between.
When preparing my talk, I was in a similar situation, is the audience perception of modern C++ the same as mine? While talking at CppCon on this, I better know where the term originated from, and what people expect about it. Maybe if we go to the roots there is a definition already? And if so, would that be the one to follow? First thing in my research to get a wider view on the beliefs on modern C++ was to ask my twitter audience in a poll:
So what is "Modern C++"?#Thursdaysurvey#cpp#cplusplus— Meeting C++ (@meetingcpp) July 28, 2016
With a little research to come up with 4 meaning full options, as twitter is only giving me 4 options. The earliest time I met the term was in 2003, when I bought Alexandrescus book "Modern C++ Design". Hence the 3. Option. A lot of people also replied to me when asking about the origins, that they would connect it with the new book from Scott Meyers most. Hence Scott could also be a good lead to understand where this term came from.
But first to the "its the new standards only" part. The argument here usually is based on C++11 language and library features. This is in my opinion wrong, C++11 has been influenced by the ideas from boost and its generic usage of templates heavily. In fact move semantics, smart pointers and other C++11 features have their roots in boost. Also the term was around for more then a decade, when C++11 finally arrived. So to me, C++11 is the first standard which reflects modern C++, but modern C++ as a style was pioneered by libraries like loki and boost.
If you read Scotts last book "Effective Modern C++", you know about lots of tricks on how to use the new things in the C++11 Standard. If you also read the acknowledgments at the beginning, you find that Scott also points towards Andrei Alexandrescu. So I asked him, and here is his answer and definition on modern C++:
Hi Jens, thanks for reaching out. My understanding is that the book "Modern C++ Design" coined the term "modern C++". The term refers to a template-intensive, generic style of writing code. The book was published in 2001. -- Andrei
There we have a definition. Its also the only definition I could find. This btw. matches also the STL, so the first C++ Standard you could say, already had (primitive) modern C++ support. It doesn't mention RAII, but his book back in 2001 covered smart pointers and also small object allocators. It shows many generic solutions to the back then very popular design patterns of the Gang of 4. The book also offers two preambles, one from Scott Meyers and one from John Vlissides, member of the Gang of 4.
Today, these examples are still good, and I think it is an excellent learning exercise to port these examples to C++11/14/17. Today variadic templates, forwarding and other language features make it easier to handle constructors, functors and generic code in general.
One interesting thing with the book is, that it does not use the standard style, it uses the style from Alexandrescus Loki Library, which also can be seen as an implementation of the ideas leading to the book.
But doing some more research for this blog article, I could not find this quote any where on the web. Google brought up another similar quote, from Alexandrescus Website:
Today, programmers colloquially use the term "modern C++" to refer to the style promoted by the book—light, precise components that use the type system gainfully to adapt to the usage context and to generate specialized code.
His book "Modern C++ Design" does by the way not include a definition of the term it self. But its fairly clear, that this book popularized the term and style. Libraries like Loki and boost were based on this idea, but also of course clearly influenced by the STL.
With this, its now clear what the term refers to. We have two quotes from Andrei Alexandrescu (2011, 2016), plus the in 2001 published book. And the trend in C++ goes to evolve generic code even further.
It is now clear, that modern C++ has a rich past and a great future in C++. Alexandrescus booked showed, that templates were useful for more then simple containers.
But my research and conversations on this also showed, that today, modern C++ can mean many things to different audiences. Some people use it to describe the new C++ standards. Some people might even use it as a buzz word.
My personal view on this, is that when you talk about modern C++, it refers to the generic way that is so centric to C++. Be it in the standard of today, or in the coming standards. C++ embraces this more and more, with every new standard we see new tools for support of new ways and making old ways easier in generic programming. enable_if and if constexpr are great examples for this. Static polymorphism and std/boost::variant is another example, one which enables lots of things, which otherwise would have to be handled at run time.
But also, there is a broad difference in understanding what this term actually should mean. Herb Sutter wrote in an article in 2011 that it is using standard C++ after C++11. Its the second popular definition of what Modern C++ should mean. So we have two different schools of thought at least for this term:
- the history meaning refering to the style named after Alexandrescus book, popularized by boost, loki and others.
- one becoming popular with C++11, coined in Herb Sutters article, broadening/changing its meaning to using standard C++ >= C++11
I can agree with with Herb, that today this includes the new standards, as long as you see the original meaning, of prefering generic code over runtime/concrete code. Writing C++ with classes in C++11 style is not Modern C++ to me.