Session 6 Summary

Code Examples for Session 06

Discussing an Implementation of sparse_array

Move Semantics Continued (part 2/3)

Move-only Types

Types that only provide move construction and move assignment and forbid any other instantiation method are called move-only types. In effect, a move-only object, and in effect its resources, cannot be cloned to another owner or shared between objects.

Smart Pointers and Move Semantics

std::unique_ptr

Reference to std::unique_ptr

std::unique_ptr maintains exclusive ownership semantics. A non-null std::unique_ptr always owns what it points to. Consequently, copying it must be forbidden as all owners would assume exclusive access to the encapsulated object. It is therefore a move-only type: moving a std::unique_ptr transfers ownership from the source pointer to the destination pointer.

std::shared_ptr

Reference to std::unique_ptr

Idioms - Patterns for the Sophisticated

Much like design patterns, idioms are common techniques that have been proven beneficial for common situations. We already discussed several idioms along the way, but there are just way too many to cover all of them in a single semester.

Some idioms we encountered so far:

… and many more.

Others, mostly those related to allocation, have been intentionally skipped.

Curiously Recurring Template Pattern

Example: Iterator Base Class Template in DASH

  • IteratorBase
    • Note that I am subclassing std::iterator here - don’t do this!
      This is because DASH is an STL implementation so we wanted our iterators to type-match STL iterators, but this is deprecated style (see note N3931 on open-std.org)
    • Default constructor is deleted to forbid direct instantiation of IteratorBase. Derived iterator classes should be default-constructible.
  • Usage of IteratorBase
    Search for IndexSetIterator. It is not default-constructible for rather peculiar reasons, don’t imitate this.