The STL design: an overview
HCM City C++ users meetup
Germán Diago Gómez
May 22nd, 2016
Germán Diago Gómez The STL design: an overview May 22nd, 2016 1 / 32
Overview Introduction
Goals of this talk
introduce STL fundamental ideas
Germán Diago Gómez The STL design: an overview May 22nd, 2016 2 / 32
Overview Introduction
Goals of this talk
introduce STL fundamental ideas
understand the design choices that drove its current design
Germán Diago Gómez The STL design: an overview May 22nd, 2016 2 / 32
Overview Introduction
Goals of this talk
introduce STL fundamental ideas
understand the design choices that drove its current design
light introduction to each of its fundamental parts
Germán Diago Gómez The STL design: an overview May 22nd, 2016 2 / 32
Overview Introduction
Goals of this talk
introduce STL fundamental ideas
understand the design choices that drove its current design
light introduction to each of its fundamental parts
Germán Diago Gómez The STL design: an overview May 22nd, 2016 2 / 32
Overview Introduction
Goals of this talk
introduce STL fundamental ideas
understand the design choices that drove its current design
light introduction to each of its fundamental parts
Germán Diago Gómez The STL design: an overview May 22nd, 2016 2 / 32
Overview Introduction
Non-goals
not a detailed, fine-grained overview
Germán Diago Gómez The STL design: an overview May 22nd, 2016 3 / 32
Overview Introduction
Non-goals
not a detailed, fine-grained overview
not lots of examples
Germán Diago Gómez The STL design: an overview May 22nd, 2016 3 / 32
Overview Introduction
Non-goals
not a detailed, fine-grained overview
not lots of examples
due to time constraints
Germán Diago Gómez The STL design: an overview May 22nd, 2016 3 / 32
Fundamentals
STL
STL stands for Standard Template Library
Germán Diago Gómez The STL design: an overview May 22nd, 2016 4 / 32
Fundamentals
STL
STL stands for Standard Template Library
it is a subset of the C++ Standard Library
Germán Diago Gómez The STL design: an overview May 22nd, 2016 4 / 32
Fundamentals
STL
STL stands for Standard Template Library
it is a subset of the C++ Standard Library
its main author is Alexander Stepanov
Germán Diago Gómez The STL design: an overview May 22nd, 2016 4 / 32
Fundamentals
STL
STL stands for Standard Template Library
it is a subset of the C++ Standard Library
its main author is Alexander Stepanov
got accepted into ISO C++98 standard library
Germán Diago Gómez The STL design: an overview May 22nd, 2016 4 / 32
Fundamentals
Design
general
Germán Diago Gómez The STL design: an overview May 22nd, 2016 5 / 32
Fundamentals
Design
general
allows a high degree of customization through composition
Germán Diago Gómez The STL design: an overview May 22nd, 2016 5 / 32
Fundamentals
Design
general
allows a high degree of customization through composition
extensible: you can add new components to the framework
Germán Diago Gómez The STL design: an overview May 22nd, 2016 5 / 32
Fundamentals
Design
general
allows a high degree of customization through composition
extensible: you can add new components to the framework
reusable without loss of performace compared to hand-written code
Germán Diago Gómez The STL design: an overview May 22nd, 2016 5 / 32
Fundamentals
Design
general
allows a high degree of customization through composition
extensible: you can add new components to the framework
reusable without loss of performace compared to hand-written code
intensive use of generic programming
Germán Diago Gómez The STL design: an overview May 22nd, 2016 5 / 32
Fundamentals
STL main components
containers
Germán Diago Gómez The STL design: an overview May 22nd, 2016 6 / 32
Fundamentals
STL main components
containers
iterators
Germán Diago Gómez The STL design: an overview May 22nd, 2016 6 / 32
Fundamentals
STL main components
containers
iterators
algorithms
Germán Diago Gómez The STL design: an overview May 22nd, 2016 6 / 32
Fundamentals
STL main components
containers
iterators
algorithms
utilities (not the focus of this talk)
Germán Diago Gómez The STL design: an overview May 22nd, 2016 6 / 32
Fundamentals
Why generic programming
allows compile-time polymorphism combined with overloading
Germán Diago Gómez The STL design: an overview May 22nd, 2016 7 / 32
Fundamentals
Why generic programming
allows compile-time polymorphism combined with overloading
allows customized code generation at compile-time
Germán Diago Gómez The STL design: an overview May 22nd, 2016 7 / 32
Fundamentals
Why generic programming
allows compile-time polymorphism combined with overloading
allows customized code generation at compile-time
allows choose best optimization based on overloading, which is a
compile-time mechanism
Germán Diago Gómez The STL design: an overview May 22nd, 2016 7 / 32
Fundamentals
Why generic programming
allows compile-time polymorphism combined with overloading
allows customized code generation at compile-time
allows choose best optimization based on overloading, which is a
compile-time mechanism
allows high degree of customization, for both algorithms and data structures
without loss of performance
Germán Diago Gómez The STL design: an overview May 22nd, 2016 7 / 32
Fundamentals
Why generic programming (2)
types can retroactively model a concept
Germán Diago Gómez The STL design: an overview May 22nd, 2016 8 / 32
Fundamentals
Why generic programming (2)
types can retroactively model a concept
example: array pointers are iterators
Germán Diago Gómez The STL design: an overview May 22nd, 2016 8 / 32
Fundamentals
Why generic programming (2)
types can retroactively model a concept
example: array pointers are iterators
OO alternative: uses run-time polymorphism and dispatch
Germán Diago Gómez The STL design: an overview May 22nd, 2016 8 / 32
Fundamentals
Why generic programming (2)
types can retroactively model a concept
example: array pointers are iterators
OO alternative: uses run-time polymorphism and dispatch
performance penalty
Germán Diago Gómez The STL design: an overview May 22nd, 2016 8 / 32
Fundamentals
Concepts briefly explained
Concepts are the foundation of generic programming
Germán Diago Gómez The STL design: an overview May 22nd, 2016 9 / 32
Fundamentals
Concepts briefly explained
Concepts are the foundation of generic programming
a Concept is a set of requirements that a type must meet
Germán Diago Gómez The STL design: an overview May 22nd, 2016 9 / 32
Fundamentals
Concepts briefly explained
Concepts are the foundation of generic programming
a Concept is a set of requirements that a type must meet
the syntactic requirements that a type must meet
Germán Diago Gómez The STL design: an overview May 22nd, 2016 9 / 32
Fundamentals
Concepts briefly explained
Concepts are the foundation of generic programming
a Concept is a set of requirements that a type must meet
the syntactic requirements that a type must meet
the semantic requirements that these syntactic requirements must meet
Germán Diago Gómez The STL design: an overview May 22nd, 2016 9 / 32
Fundamentals
Concepts briefly explained
Concepts are the foundation of generic programming
a Concept is a set of requirements that a type must meet
the syntactic requirements that a type must meet
the semantic requirements that these syntactic requirements must meet
optionally, associated types for that concept
Germán Diago Gómez The STL design: an overview May 22nd, 2016 9 / 32
Fundamentals
Concepts briefly explained
Concepts are the foundation of generic programming
a Concept is a set of requirements that a type must meet
the syntactic requirements that a type must meet
the semantic requirements that these syntactic requirements must meet
optionally, associated types for that concept
algorithms: include the big O cost associated to them
Germán Diago Gómez The STL design: an overview May 22nd, 2016 9 / 32
Fundamentals
Concepts briefly explained
Concepts are the foundation of generic programming
a Concept is a set of requirements that a type must meet
the syntactic requirements that a type must meet
the semantic requirements that these syntactic requirements must meet
optionally, associated types for that concept
algorithms: include the big O cost associated to them
In C++ these are the requirements that your template parameters need to
fullfill. They are stated in the documentation, since there is no language
support (yet) for Concepts
Germán Diago Gómez The STL design: an overview May 22nd, 2016 9 / 32
Fundamentals
Concepts briefly explained (2)
similar to the way a virtual function sets requirements on derived types
Germán Diago Gómez The STL design: an overview May 22nd, 2016 10 / 32
Fundamentals
Concepts briefly explained (2)
similar to the way a virtual function sets requirements on derived types
but at compile-time
Germán Diago Gómez The STL design: an overview May 22nd, 2016 10 / 32
Fundamentals
Concepts briefly explained (2)
similar to the way a virtual function sets requirements on derived types
but at compile-time
when a type fullfills the requirements of a concept we say it models that
concept
Germán Diago Gómez The STL design: an overview May 22nd, 2016 10 / 32
Fundamentals
Concepts briefly explained (2)
similar to the way a virtual function sets requirements on derived types
but at compile-time
when a type fullfills the requirements of a concept we say it models that
concept
similar to when we say a derived class implements requirements from a base
class
Germán Diago Gómez The STL design: an overview May 22nd, 2016 10 / 32
Fundamentals
Concepts briefly explained (2)
similar to the way a virtual function sets requirements on derived types
but at compile-time
when a type fullfills the requirements of a concept we say it models that
concept
similar to when we say a derived class implements requirements from a base
class
types do not need to inherit from any class to model a Concept → more
loosely coupled
Germán Diago Gómez The STL design: an overview May 22nd, 2016 10 / 32
Fundamentals
Concepts example: LessThanComparable
syntactic requirements
Germán Diago Gómez The STL design: an overview May 22nd, 2016 11 / 32
Fundamentals
Concepts example: LessThanComparable
syntactic requirements
your type must work with operator< and return a type convertible to bool
Germán Diago Gómez The STL design: an overview May 22nd, 2016 11 / 32
Fundamentals
Concepts example: LessThanComparable
syntactic requirements
your type must work with operator< and return a type convertible to bool
semantic requirements for LessThanComparable operator<
Germán Diago Gómez The STL design: an overview May 22nd, 2016 11 / 32
Fundamentals
Concepts example: LessThanComparable
syntactic requirements
your type must work with operator< and return a type convertible to bool
semantic requirements for LessThanComparable operator<
∀a, !(a < a)
Germán Diago Gómez The STL design: an overview May 22nd, 2016 11 / 32
Fundamentals
Concepts example: LessThanComparable
syntactic requirements
your type must work with operator< and return a type convertible to bool
semantic requirements for LessThanComparable operator<
∀a, !(a < a)
a < b ⇐⇒ !(b < a)
Germán Diago Gómez The STL design: an overview May 22nd, 2016 11 / 32
Fundamentals
Concepts example: LessThanComparable
syntactic requirements
your type must work with operator< and return a type convertible to bool
semantic requirements for LessThanComparable operator<
∀a, !(a < a)
a < b ⇐⇒ !(b < a)
a < b ∧ b < c =⇒ a < c
Germán Diago Gómez The STL design: an overview May 22nd, 2016 11 / 32
Fundamentals
Concepts example: LessThanComparable
syntactic requirements
your type must work with operator< and return a type convertible to bool
semantic requirements for LessThanComparable operator<
∀a, !(a < a)
a < b ⇐⇒ !(b < a)
a < b ∧ b < c =⇒ a < c
associated types (optionally)
Germán Diago Gómez The STL design: an overview May 22nd, 2016 11 / 32
Fundamentals
More Concepts examples
BinaryPredicate
DefaultConstructible
Many others
Germán Diago Gómez The STL design: an overview May 22nd, 2016 12 / 32
Containers
Introduction
Containers allow you to store collections of data in different data structures that
best suite your needs.
Germán Diago Gómez The STL design: an overview May 22nd, 2016 13 / 32
Containers
STL containers classification
sequence containers
Germán Diago Gómez The STL design: an overview May 22nd, 2016 14 / 32
Containers
STL containers classification
sequence containers
std::array<T>, std::vector<T>, std::list<T>. . .
Germán Diago Gómez The STL design: an overview May 22nd, 2016 14 / 32
Containers
STL containers classification
sequence containers
std::array<T>, std::vector<T>, std::list<T>. . .
associative containers
Germán Diago Gómez The STL design: an overview May 22nd, 2016 14 / 32
Containers
STL containers classification
sequence containers
std::array<T>, std::vector<T>, std::list<T>. . .
associative containers
ordered → std::set. . .
Germán Diago Gómez The STL design: an overview May 22nd, 2016 14 / 32
Containers
STL containers classification
sequence containers
std::array<T>, std::vector<T>, std::list<T>. . .
associative containers
ordered → std::set. . .
unordered → std::unordered_map. . .
Germán Diago Gómez The STL design: an overview May 22nd, 2016 14 / 32
Containers
STL containers classification
sequence containers
std::array<T>, std::vector<T>, std::list<T>. . .
associative containers
ordered → std::set. . .
unordered → std::unordered_map. . .
container adaptors
Germán Diago Gómez The STL design: an overview May 22nd, 2016 14 / 32
Containers
STL containers classification
sequence containers
std::array<T>, std::vector<T>, std::list<T>. . .
associative containers
ordered → std::set. . .
unordered → std::unordered_map. . .
container adaptors
std::priority_queue, std::stack. . .
Germán Diago Gómez The STL design: an overview May 22nd, 2016 14 / 32
Containers
STL containers traversal
depends on the kind iterators provided (more later)
Germán Diago Gómez The STL design: an overview May 22nd, 2016 15 / 32
Containers
STL containers traversal
depends on the kind iterators provided (more later)
random access, bidirectional, forward containers, etc.
Germán Diago Gómez The STL design: an overview May 22nd, 2016 15 / 32
Iterators
Introduction
Iterators are used to traverse ranges
Germán Diago Gómez The STL design: an overview May 22nd, 2016 16 / 32
Iterators
Introduction
Iterators are used to traverse ranges
Iterators are an abstraction for positions
Germán Diago Gómez The STL design: an overview May 22nd, 2016 16 / 32
Iterators
Introduction
Iterators are used to traverse ranges
Iterators are an abstraction for positions
they are modeled after C pointers in C++’s concrete case
Germán Diago Gómez The STL design: an overview May 22nd, 2016 16 / 32
Iterators
Introduction
Iterators are used to traverse ranges
Iterators are an abstraction for positions
they are modeled after C pointers in C++’s concrete case
you can do a traversal with two iterators
Germán Diago Gómez The STL design: an overview May 22nd, 2016 16 / 32
Iterators
Introduction
Iterators are used to traverse ranges
Iterators are an abstraction for positions
they are modeled after C pointers in C++’s concrete case
you can do a traversal with two iterators
they are not the same as an iterator in other languages
Germán Diago Gómez The STL design: an overview May 22nd, 2016 16 / 32
Iterators
Introduction
Iterators are used to traverse ranges
Iterators are an abstraction for positions
they are modeled after C pointers in C++’s concrete case
you can do a traversal with two iterators
they are not the same as an iterator in other languages
Java/C# iterator similar to a pair of iterators in C++
Germán Diago Gómez The STL design: an overview May 22nd, 2016 16 / 32
Iterators
Introduction
Iterators are used to traverse ranges
Iterators are an abstraction for positions
they are modeled after C pointers in C++’s concrete case
you can do a traversal with two iterators
they are not the same as an iterator in other languages
Java/C# iterator similar to a pair of iterators in C++
a pair of iterators is usually called a range in C++
Germán Diago Gómez The STL design: an overview May 22nd, 2016 16 / 32
Iterators
Iterator categories
several kinds of iterators
Germán Diago Gómez The STL design: an overview May 22nd, 2016 17 / 32
Iterators
Iterator categories
several kinds of iterators
iterators form a hierarchy of Concepts
Germán Diago Gómez The STL design: an overview May 22nd, 2016 17 / 32
Iterators
Iterator categories
several kinds of iterators
iterators form a hierarchy of Concepts
not implemented as inheritance in the library
Germán Diago Gómez The STL design: an overview May 22nd, 2016 17 / 32
Iterators
Iterator categories
several kinds of iterators
iterators form a hierarchy of Concepts
not implemented as inheritance in the library
they are Concepts
Germán Diago Gómez The STL design: an overview May 22nd, 2016 17 / 32
Iterators
Iterator categories
several kinds of iterators
iterators form a hierarchy of Concepts
not implemented as inheritance in the library
they are Concepts
RandomAccessIterator → BidirectionalIterator →
ForwardIterator → InputIterator
Germán Diago Gómez The STL design: an overview May 22nd, 2016 17 / 32
Iterators
Iterator categories
several kinds of iterators
iterators form a hierarchy of Concepts
not implemented as inheritance in the library
they are Concepts
RandomAccessIterator → BidirectionalIterator →
ForwardIterator → InputIterator
OutputIterator is any iterator that can write to the element it points to
Germán Diago Gómez The STL design: an overview May 22nd, 2016 17 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
increment iterator position by one
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
increment iterator position by one
single traversal
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
increment iterator position by one
single traversal
ForwardIterator
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
increment iterator position by one
single traversal
ForwardIterator
InputIterator + support for multiple traversals
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
increment iterator position by one
single traversal
ForwardIterator
InputIterator + support for multiple traversals
BidirectionalIterator
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
increment iterator position by one
single traversal
ForwardIterator
InputIterator + support for multiple traversals
BidirectionalIterator
ForwardIterator + supports decrementing position by one
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
increment iterator position by one
single traversal
ForwardIterator
InputIterator + support for multiple traversals
BidirectionalIterator
ForwardIterator + supports decrementing position by one
RandomAccessIterator
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
increment iterator position by one
single traversal
ForwardIterator
InputIterator + support for multiple traversals
BidirectionalIterator
ForwardIterator + supports decrementing position by one
RandomAccessIterator
BidirectionalIterator + supports jumping to arbitrary position in O(1)
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Iterator categories (2)
every iterator category supports a set of operations
InputIterator
element access
increment iterator position by one
single traversal
ForwardIterator
InputIterator + support for multiple traversals
BidirectionalIterator
ForwardIterator + supports decrementing position by one
RandomAccessIterator
BidirectionalIterator + supports jumping to arbitrary position in O(1)
think of a pointer to C array position
Germán Diago Gómez The STL design: an overview May 22nd, 2016 18 / 32
Iterators
Why are iterators important
abstract traversal from the underlying data structure
Germán Diago Gómez The STL design: an overview May 22nd, 2016 19 / 32
Iterators
Why are iterators important
abstract traversal from the underlying data structure
decouple algorithms implementation from data structure (later more)
Germán Diago Gómez The STL design: an overview May 22nd, 2016 19 / 32
Iterators
Why are iterators important
abstract traversal from the underlying data structure
decouple algorithms implementation from data structure (later more)
can implement algorithms based on its iterator category (later more)
Germán Diago Gómez The STL design: an overview May 22nd, 2016 19 / 32
Iterators
Why are iterators important
abstract traversal from the underlying data structure
decouple algorithms implementation from data structure (later more)
can implement algorithms based on its iterator category (later more)
containers just need to provide a begin/end sequence pair
Germán Diago Gómez The STL design: an overview May 22nd, 2016 19 / 32
Iterators
Why are iterators important
abstract traversal from the underlying data structure
decouple algorithms implementation from data structure (later more)
can implement algorithms based on its iterator category (later more)
containers just need to provide a begin/end sequence pair
let you reduce implementation effort on algorithms
Germán Diago Gómez The STL design: an overview May 22nd, 2016 19 / 32
Algorithms
Introduction
algorithms let you perform operations on your data
Germán Diago Gómez The STL design: an overview May 22nd, 2016 20 / 32
Algorithms
Introduction
algorithms let you perform operations on your data
STL algorithms design choices are a bit suprising at first
Germán Diago Gómez The STL design: an overview May 22nd, 2016 20 / 32
Algorithms
Algorithm design choices
it was decided to implement algorithms as free functions, not as member
functions
Germán Diago Gómez The STL design: an overview May 22nd, 2016 21 / 32
Algorithms
Algorithm design choices
it was decided to implement algorithms as free functions, not as member
functions
it was decided that it would take Iterator s as input, instead of Container
s
Germán Diago Gómez The STL design: an overview May 22nd, 2016 21 / 32
Algorithms
Algorithm design choices
it was decided to implement algorithms as free functions, not as member
functions
it was decided that it would take Iterator s as input, instead of Container
s
consequence: algorithms are decoupled from containers
Germán Diago Gómez The STL design: an overview May 22nd, 2016 21 / 32
Algorithms
STL algorithms/iterators conventions
algorithm specifications use half-open ranges for algorithms
Germán Diago Gómez The STL design: an overview May 22nd, 2016 22 / 32
Algorithms
STL algorithms/iterators conventions
algorithm specifications use half-open ranges for algorithms
(a, b]
Germán Diago Gómez The STL design: an overview May 22nd, 2016 22 / 32
Algorithms
STL algorithms/iterators conventions
algorithm specifications use half-open ranges for algorithms
(a, b]
a is included in the range. b is not included in the range
Germán Diago Gómez The STL design: an overview May 22nd, 2016 22 / 32
Algorithms
STL algorithms/iterators conventions
algorithm specifications use half-open ranges for algorithms
(a, b]
a is included in the range. b is not included in the range
containers provide begin, end and its variants
Germán Diago Gómez The STL design: an overview May 22nd, 2016 22 / 32
Algorithms
STL algorithms/iterators conventions
algorithm specifications use half-open ranges for algorithms
(a, b]
a is included in the range. b is not included in the range
containers provide begin, end and its variants
begin points to the first element of the container
Germán Diago Gómez The STL design: an overview May 22nd, 2016 22 / 32