36

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++?

1
  • 4
    Without actual quotes or references, it's hard to know exactly what you're talking about. Can you provide a link or a reference so we know what you read?
    – S.Lott
    Sep 7, 2010 at 18:08

5 Answers 5

44

Extensive use of standard library and STL, exceptions and templates - rather than just C with classes

6
  • @Martin Beckett STL is a part of standard library. Sep 7, 2010 at 19:33
  • 2
    @A-ha, STL is often used as an informal way to refer to the "new" parts of the standard library. Sep 7, 2010 at 19:49
  • 6
    I'd add RAII to the list too. Sep 7, 2010 at 19:52
  • 1
    @A-ha STL is/is-not std lib always appears to upset pedants on here. The point was that new C++ is stringstream, old C++ is printf() ! Sep 7, 2010 at 20:06
  • 1
    @Martin and it is very positive characteristic to be a pedantic in programming profession. When someone is sloppy it just doesn't help. You've just made formal mistake and I've corrected you, that's all. And when you saying: "The point was that new C++ is stringstream, old C++ is printf() " is just a very cheap excuse of a child who got caught. No need for exclamation mark though (it's awfull give away that you are irritated). Sep 8, 2010 at 7:41
37

"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 new and delete
  • 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.

4
  • 1
    I would argue that unfortunately modern C++ isn't afraid of exceptions. Not that I don't like exceptions, it's just that people abuse them... Sep 8, 2010 at 6:23
  • @Matthieu, unfortunately more programmers aren't afraid of them when they should be. Didn't Raymond Chen (of Microsoft) famously declare that he isn't smart enough to use them? Sep 8, 2010 at 11:48
  • 2
    I think he did it to shock :) But exceptions do introduce jumps in the code that make it difficult to follow the execution path, especially because those are hidden jumps. If reserved to truly exceptional conditions, then it doesn't matter, but if any operator invoked may fail, then you're in for a world of hurt (maintenance-wise). I have decided (quite recently) to try and program exception-free whenever I could. After all, it is normal for a find method NOT to find a result. Sep 8, 2010 at 13:28
  • Exceptions just move complexity around: they keep the normal operation code line looking simple, but make exception handling more complex by divorcing the code that does it from the code that caused the problem. Without a good exception code/object it can be really hard to sort out what happened in an exception handler, and quite often you don't get one. Traditional return code-style error propagation makes the main line hard to read but exception handling is right next to the guilty code. Really exceptions are just another glorified goto. Nov 4, 2018 at 21:33
21

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.

0
14

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.

2
  • 1
    what's even more remarkable is that templates were never intended for how they are used. It's not like Stroustrup set out to create a turing-complete sublanguage that executes at compile-time. Sep 8, 2010 at 11:46
  • @Kristo: I agree, it was discovered by accident that they formed a Turing Complete language and I am still discovering new interesting usages on a monthly basis (not by myself :p I do read a lot). What's even more amusing is that they introduce Functional Programming idioms in C++ because of the inherent immutability of types in C++ :) Sep 8, 2010 at 13:30
3

The Wikipedia page on the upcoming C++ standard is not a bad start for reading up on newer C++.

Not the answer you're looking for? Browse other questions tagged or ask your own question.