Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

These are mechanisms that are usually useful but can be harmful if over used.

  • auto - Use when the type isn't obvious or the variable is used only in a limited single scope (e.g. for loop variables). Remember that using auto means no one reviewing the code will know the type either. The most command and best use is as the type for iterators in a for loop, but use of the container style for loop is even better.
  • lambda - Useful for one shot functions that are only used locally. This can make std::for_each
  • Initializer lists and uniform initialization - handy for disambiguating initializations and improving over all code consistency.
  • Cross construction invocation - This can clean up code if a class has a variety of constructors and all of them initialize the same part of the class in the same way. Rather than cutting and pasting or introducing an artificial init() method that initialization can be delegated to a base constructor. In many cases, however, direct member initialization can do this as well and that is the preferred mechanism.
  • Enum raw type control - it is now possible to control the underlying type of an enum. This is useful in cases where the size matters (e.g., byte vs. 4 bytes).

...

These mechanisms are useful in more limited circumstances and if used improperly can cause problems. They require a deeper understanding of C++ mechanics to use effectively. If you are unsure about whether to use any of these, don't.

  • std::move and move constructors - These can greatly increase efficiency when used correctly. When used inappropriately these can use the sort of problems that caused auto_ptr to be removed from the language. The classic use case is for objects that contain a single or small set of pointers which can be cheaply moved in comparison to allocating and copying the pointed to data.
  • Enum classes - this prevents enum names from "leaking" in to the surrounding scope which can be good or bad depending on circumstances.
  • std::function - this captures function objects so that methods, free functions, lambdas, and functors can be handled uniformly. Interfaces that accept callbacks can use this to simplify both the interface and the internal implementation while providing more choice to the caller.

...