Nobody Understands

Nobody Understands C++: Part 9: Error Handling

I was recently at a talk where the speaker was discussing the history of C++. He argued that one problem with C++ was that its design requirements included backward compatibility with C code, and one fallout of this was the requirement to support all previous types of error handling as well as adding exceptions. That is, C++ supports:

  1. Returning of error codes.
  2. Error handling functions.
  3. Exception handling.

It is true that C++ supports all 3 of these mechanisms, as well as some other cruft left behind from C that unnecessarily encumbers the language. The speaker's point on this one particular case, however, is lost to the fact that every language supports the first two.

Nobody Understands C++: Part 8: Operator Overloading

There has been much discussion over the years about the usefulness of operator overloading in C++ as well as the ability to get it right.

In reality, it's hard to get it wrong as long as you follow the canonical forms of the operators and don't do unexpected things like overloade the + operator to perform a subtraction operation. Also, the types that your overloaded operators work with should be consistent.

Nobody Understands C++: Part 7: C++ Coding Standards

In case you find yourself asking where I get the stuff from that I blog about and wondering if I'm just making stuff up; many of the "Nobody Understands C++" articles on this website are inspired by principles found in the book "C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu. The book is a collection of items from previous books that the pair has worked on together. As such, if you do not own any of the previous books this one makes a great addition to your library.

The book covers 101 best practices guidelines for your C++ code. It does not cover things like indentation level and style of comments, the things that we have all come to consider to be part and parcel with a standards document. In fact, it explicitly avoids talking about those things and says that they should be internally consistent within each file. "C++ Coding Standards" covers topics on program design, object lifetime management, namespaces, generic programming (templates) and the standard library.

Nobody Understands C++: Part 6: Are You Still Using Pointers?

It is best to avoid using pointers in C++ as much as possible. The use of pointers can lead to confusion of ownership which can directly or indirectly lead to memory leaks. Even if object ownership is well managed simple (and difficult to find) bugs can also lead to memory leaks.

Security Risks

Also, by avoiding the use of pointers we can avoid and sometimes eliminate common security holes such as buffer overruns. See the last example for more information about this.

Examples

Common instances were pointer usage can be avoided:

Top 5 Myths About C++

We've now completed 5 articles in the "Nobody Understands C++" series so here we are going to recap the misconceptions we have covered.

  1. You shouldn't use all the language features of C++, that just makes your code too complicated! Wrong, we learned that by using some of the less used features of C++ we can make our code much more succinct.

Nobody Understands C++: Part 5: Template Code Bloat

On occasion you will read or hear someone talking about C++ templates causing code bloat. I was thinking about it the other day and thought to myself, "self, if the code does exactly the same thing then the compiled code cannot really be any bigger, can it?"

Here are the test cases presented, first without the use of templates, second with the use of templates. Exactly the same functionality and exactly the same code output:

Syndicate content