Chapter 29. Ten Major Recent Additions to C++

In This Chapter

  • Using smart pointers

  • Initializing objects with a variable-length list

  • Initializing data members inline

  • Instantiating an extern template

  • Implementing thread local storage

  • Using r-value references to avoid creating copies of objects

  • Implementing concepts

  • Defining lamda expressions

  • Defining variadic templates

  • Using typeid

Note

In this chapter, I look at ten major additions to the C++ language. Most (but not all) of these additions weren't made formal until the release of the C++ 2009 standard. All of these topics are beyond the range of an introductory book; I merely touch on these topics to give you an idea of where you might want to study next.

Use Smart Pointers

Loss of memory due to forgetting to return memory to the heap is one of the biggest sources of error in C++ programs. Both Java and C#, two followers to the C++ language, instituted a process known as garbage collection that collects unreferenced memory and returns it to the heap for you. Garbage collection has been discussed for C++ as well but has been rejected for several reasons, including the overhead it levies on your program.

The problem could be "solved" if dynamically allocated memory were tied to an object rather than a pointer. The destructor of an object is invoked when it goes out of scope. This destructor could keep track of when it was appropriate to return the memory to the heap.

The standard library includes a shared_ptr<T> template class, which allocates memory ...

Get C++ For Dummies®, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.