Chapter 4. Maximally Reusable Generic Containers—Part 1

Difficulty: 8

How flexible can you make this simple container class? Hint: You'll learn more than a little about member templates along the way.

How can you best implement copy construction and copy assignment for the following fixed-length vector class? How can you provide maximum usability for construction and assignment? Hint: Think about the kinds of things that client code might want to do.

 template<typename T, size_t size> class fixed_vector { public: typedef T* iterator; typedef const T* const_iterator; iterator begin() { return v_; } iterator end() { return v_+size; } const_iterator begin() const { return v_; } const_iterator end() const { return v_+size; } private: T v_[size]; };  ...

Get Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.