-
A resource is something that must be acquired and later released
- Examples: Memory, locks, sockets, thread handles, file handles
mutex
:lock
,unlock
unique_lock
can wrap a mutex. It locks it on initialization and releases it in the destructor- Smart pointers
unique_ptr
,shared_ptr
manage objects stored on the heap - The object is released when the smart pointer is out of scope and gets destroyed
shared_ptr
: The object is destroyed when the last reference is out of scope. It uses copy semantics rather than move semanticsmake_unique
,make_shared
are helper functions for creating smart pointersauto x = make_unique<A>(args)
is shorthand forunique_ptr<A> x { new A(args) }
- These wrappers make it possible to enforce a no naked new policy
T[n]
is a standard arrayarray<T, n>
wraps this for convenience and provides better type safetybitset<n>
pair<A, B>
for two values of arbitrary typestuple<T, …>
for an arbitrary number of values with arbitrary typesmake_pair
andmake_tuple
for creating pairs and tuples without explicitly stating their typestd::get<i>(tuple)
to retrieve thei
-th element of a tuple. This provides type safety
std::chrono
time_point
for timestamps- Difference of two
time_point
s is aduration
- Functions for casting between different time units
bind
for function curryingplaceholders
contains_1
, etc for declaring what arguments to setbind(f, 2, _1)
sets the first argument to2
- If
f
is overloaded, we have to specify which one to use mem_fn
lets us call a member function as a non-member. This is useful for passing it into other functionsauto draw = mem_fn(&Shape::draw); for_each(v.begin(), v.end(), draw)
function
lets us declare the type of a function. For example,function<int(double)>
is the type of all functions that take a double and return an int
- Type functions are functions that take a type as input or return one as output. They are evaluated at compile time
numeric_limits
from<limits>
provides the min/max values of certain typessizeof
is another example- Sometimes we want different implementations for iterators with different traits.
Iterator_category<IterationType>
constructs a tag that indicates what kind of iterator it is. We can then use function overloading with pattern matching to write fitting implementations <type_traits>
lets us check for traits of types, e.g. whether they are arithmetic. This in conjunction withassert
lets us declare what types are allowed in our templates