Best Practices

When to use class vs. typename in a Template Declaration

When declaring a template you can choose either "class" or "typename" for a template parameter.

Example:

template<typename T>
class vector
{
  // ...
};

or

When to use reinterpret_cast<> vs. static_cast<>

The boost developers mailing list recently discussed the differences between reinterpret_cast<> and static_cast<> C++ casting operators.

The general consensus is that reinterpret_cast<> provides virtually no guarantees, does not have portable behavior and should only be used when you are trying to perform a cast that you should basically not be performing in the first place. The only legitimate use for reinterpret_cast<> seems to be converting between integral types and back, for instance, from a char * to an int and back to a char *.

static_cast<> should be used for virtually anything that dynamic_cast<> and const_cast<> cannot do.

When to use #include "" verses #include <>

Different compilers treat the two types of includes:

#include "filename"

and

#include <filename>

Differently in the details, but if you are programming for portability there is a simple rule of thumb to keep in mind: use the <> version to reference installed files, not local files.

We will illustrate the reasoning for using the #include "" format in your library's header files below.

myproject

src

project.cpp

include

Syndicate content