tags:

views:

374

answers:

7

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

+3  A: 

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

Dirk Eddelbuettel
A: 

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.

Bill K
+13  A: 

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

Martin Beckett
I'd add metaprogramming
aaa
@Martin Beckett STL is a part of standard library.
There is nothing we can do
@A-ha, STL is often used as an informal way to refer to the "new" parts of the standard library.
Mark Ransom
I'd add RAII to the list too.
Mark Ransom
@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() !
Martin Beckett
@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).
There is nothing we can do
@Mark Ransom When explaining something quite specific as notion of standard library it is better not to use "informal" way which are sometimes used by people who are not exactly sure what they talking about. Stick to the facts.
There is nothing we can do
+8  A: 

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.

Nemanja Trifunovic
+1 There's not a single `delete` in our main product.
Cubbi
@Cubbi: Ah, but how many `new`s?
John Dibling
@John Dibling: 624 `new`s right now.
Cubbi
A: 

He probably means the Alexandrescu book: amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315

paul_71
Seems very unlikely to me.
John Dibling
+7  A: 

"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.

Kristo
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...
Matthieu M.
@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?
Kristo
@Kristo: 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.
Matthieu M.
+4  A: 

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.

Matthieu M.
@Matthieu, 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.
Kristo
@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++ :)
Matthieu M.