Represents types that are generic containers of two elements.
This concept basically represents types that are like std::pair
. The motivation for making such a precise concept is similar to the motivation behind the Sequence
concept; there are many different implementations of std::pair
in different libraries, and we would like to manipulate any of them generically.
Since a Product
is basically a pair, it is unsurprising that the operations provided by this concept are getting the first and second element of a pair, creating a pair from two elements and other simmilar operations.
first
, second
and make
first
and second
must obviously return the first and the second element of the pair, respectively. make
must take two arguments x
and y
representing the first and the second element of the pair, and return a pair p
such that first(p) == x
and second(p) == y
.
For a model P
of Product
, the following laws must be satisfied. For every data types X
and Y
, there must be a unique function \( \mathtt{make} : X \times Y \to P \) such that for every x
, y
,
This is basically saying that a Product
must be the most general object able to contain a pair of objects (P1, P2)
, but nothing more. Since the categorical product is defined by a universal property, all the models of this concept are isomorphic, and the isomorphism is unique. In other words, there is one and only one way to convert one Product
to another.
Another property that must be satisfied by first
and second
is that of move-independence, which ensures that we can optimally decompose a Product
into its two members without making redundant copies.
Comparable
(free model)x
and y
are equal iff they are equal element-wise, by comparing the first element before the second element. Orderable
(free model)Foldable
(free model)Product
p
is equivalent to folding a list containing first(p)
and second(p)
, in that order.Variables | |
constexpr auto | boost::hana::first |
Returns the first element of a pair. More... | |
constexpr auto | boost::hana::second |
Returns the second element of a pair. More... | |
|
constexpr |
#include <boost/hana/fwd/first.hpp>
Returns the first element of a pair.
Note that if the Product
actually stores the elements it contains, hana::first
is required to return a lvalue reference, a lvalue reference to const or a rvalue reference to the first element, where the type of reference must match that of the pair passed to first
. If the Product
does not store the elements it contains (i.e. it generates them on demand), this requirement is dropped.
|
constexpr |
#include <boost/hana/fwd/second.hpp>
Returns the second element of a pair.
Note that if the Product
actually stores the elements it contains, hana::second
is required to return a lvalue reference, a lvalue reference to const or a rvalue reference to the second element, where the type of reference must match that of the pair passed to second
. If the Product
does not store the elements it contains (i.e. it generates them on demand), this requirement is dropped.