Share

C++ Standard Template Library - Advanced Topics

Microsoft's Stephan T. Lavavej (STL) takes you through advanced topics of the Standard Template Library (also STL). The 10 part introduction to STL series is a prerequisite for these videos.

  1. shared_ptr internals
  2. Template Meta Programming
  3. STL Validation in Debug Mode
  4. More about Rvalue References
  5. An Introduction to Boost Libraries
  6. Generic Pretty Printer for STL Containers

Advanced STL 1: shared_ptr internals

  • Introduction to shared_ptr and unique_ptr video is a prerequisite
  • shared_ptr can work in two modes:
    • Explicit new is called and the pointer is saved into a shared_ptr
    • make_shared (C++11) is used to construct the object.
  • make_shared should be the preferred approach as a single buffer is used to store the shared_ptr's internal housekeeping as well as the user allocated memory
    • The explicit new approach requires allocation of two buffers; one for internal housekeeping and second one for user allocated memory
  • shared_ptr internally reference counts the pointers to the allocated memory. Memory is freed when the reference counter drops to 0
  • weak_ptr and shared_ptr interplay is also discussed.

Advanced STL 2: Template Meta Programming

  • Partitioning algorithm
    • Changed from bidirectional iterators to forward direction iterator in C++11
    • STL uses a faster algorithm when a forward direction iterator is passed
      • This is accomplished with template meta programming.
      • The algorithm selection is done at compile time, not runtime.
  • Optimizing algorithms for specific user types
    • For example, memcmp can be used to compare containers if the STL container contained integer
    • Again, the memcmp selection is done at compile time

Advanced STL 3: STL Validation in Debug Mode

  • This tutorial describes the debug checks that are added in the debug mode build in Visual Studio.
  • Iterator debugging support in Visual Studio is covered.
  • An undocumented compiler switch for analyzing the class layout is described towards the end of the video (about 5:00 minutes from the end)
    • /dlreportSingleClassLayout<ClassName>
  • You may skip this video if you do not use Visual Studio for your C++ development

Advanced STL 4: More about Rvalue References

  • An introduction to Rvalue references is a prerequisite to this video.
  • Rvalue references enable support move semantics and perfect forwarding
  • With C++11, adding a move constructor and a move assignment operator for classes will speed up STL execution as the compiler can use move semantics for temporaries.
    • C++11 adds push_back method that takes Rvalue reference as an input. The compiler will use this method when it knows for sure that the object being passed is a temporary
  • Normally, LValue references cannot bind to RValue references
    • There is special handling for temporaries created from conversion. A temporary created from a Lvalue reference can bind to a Rvalue reference.

Advanced STL 5: An Introduction to Boost Libraries

  • Installing and navigating through Boost
  • A bimap is a data structure that represents bidirectional relations between elements of two collections.
  • Boost file system is described with an example
    • Recursive directory Iterator is used for manipulating files in a directory
    • Directory iterators can be passed to STL algorithms
    • Portable way of dealing with files

Advanced STL 6: Generic Pretty Printer for STL Containers

  • A pretty printer for STL containers is built using template meta programming techniques.
  • Vectors, sets, maps and tuples are supported.
  • Nested lists can be easily printed.
  • Learn how to write code that will target pretty printing of specific data structures.
  • Jump to 40:40 if you just want to see how to use the pretty printer.
  • pretty_printer.cpp can be downloaded from here.