Item 53. Embedded Type Information

How do we know the type of a container’s elements?

template <typename T>class Seq {    //...};

At first, this may not seem to be a problem. The element type of Seq<std::string> is std::string, right? Not necessarily. There’s nothing to prevent the implementation of our (nonstandard) sequence container from making the element type const T, T *, or a “smart pointer” to a T. (A particularly weird container could simply ignore the template parameter and always set the element type to void *!) But vagary of implementation is not the only reason we may not be able to determine the element type of our container. We often write generic code in which that information is simply not available.

In the process generic ...

Get C++ Common Knowledge: Essential Intermediate Programming 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.