I sometimes read discussion on why C++ is good or bad and sometimes one of the arguments cites that today's modern C++ is very different from the old C++. I am wondering exactly what the difference would be? What would be an example of 'Modern' C++ and what would be an example (preferably doing the same thing) of this 'old' C++?
The Wikipedia page on the upcoming C++ standard is not a bad start for reading up on newer C++.
The main difference seems to be the inclusion of smart pointers and "Memory Management". It's extremely difficult to think in OO when you have to track object instances for deletion.
I don't believe the language itself is much different, just the libraries.
Extensive use of standard library and STL, exceptions and templates - rather than just C with classes
One very obvious difference is that in "old-style" C++ you will see many objects manually created with new and destroyed with delete. In modern C++, an object is created on the stack whenever possible, or at least wrapped within some sort of a smart pointer.
Another difference is that old style C++ focuses more on OOP, while modern C++ uses a mix of programming styles: procedural, modular, object, and generic. Free functions are considered a good thing in modern C++, whereas they would be shoved into some class in old style C++.
Other obvious differences include use of constructs and libraries that became mature and stable enough to be used in production code: templates, exceptions, namespaces, STL, etc.
He probably means the Alexandrescu book: amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315
"Modern" C++ isn't afraid to use any or all of the following:
- RAII
- standard library containers and algorithms
- templates
- metaprogramming
- exceptions
- Boost
"Old" C++ tends to avoid these things due to a perceived lack of compiler support or run-time performance. Instead, you'll find...
- lots of
newanddelete - roll-your-own linked lists and other data structures
- return codes as a mechanism for error handling
- one of the millions of custom string classes that aren't
std::string
As with all this-vs-that arguments, there are merits to both approaches. Modern C++ isn't universally better. Embedded enviornments, for example, often require extra restrictions that most people never need, so you'll see a lot of old-style code there. Overall though, I think you'll find that most of the modern features are worth using regularly. Moore's Law and compiler improvements have taken care of the majority of reasons to avoid the new stuff.
There are various things in C++ that are considered modern.
First of all, I think, is the extraordinary advent of templates. Not only the STL itself, but also the less "regular" uses of templates which have led to the development of template meta-programming. Note for example the presence of enable_if in the upcoming version of the standard.
This is the most remarkable trait I think of a movement among C++ programmer who seek to enforce correctness by construction:
- prefer compiler errors to runtime errors > templates / type safety instead of ellipsis /
void* - use Scope Bound Resource Management (aka RAII, but a tad more explicit)
This research for high quality has also led to a pervasive use of:
- the STL (algorithms and data-structures that have been thoroughly tested, even though the STL is quite unsafe by design unfortunately)
- the Boost libraries (reviewed by expert programmers, quasi bug-free, highly portable)
Reviewing these libraries also demonstrate that C++ programmers no longer have a complex of inferiority toward those OO languages: now freed from this complex, we do not hesitate to mix various paradigms (OO, generic, procedural) to achieve our goal.
All in all, I think that Modern C++ is more of a mindset. We (C++ programmers) try to free ourselves from the dreaded undefined behavior that has plagued us for so long, and try to free our users of it as well (by defining interfaces that do not allow it). We also accept the fact that others have come before, and that reusing existing libraries is NOT a sign of weakness.