template<class F, class S>
Corrade::Containers::Pair class new in Git master

Pair of values.

An alternative to std::pair that is trivially copyable for trivial types, provides move semantics consistent across standard library implementations and guarantees usability in constexpr contexts even in C++11. On the other hand, to simplify both the implementation and usage semantics, the type doesn't support references — wrap them in a Reference in order to store them in a Pair. Such type composition allows you to both rebind the reference and update the referenced value and the intent is clear.

Similarly to other containers and equivalently to std::make_pair(), there's also pair(). The following two lines are equivalent:

auto a = Containers::Pair<float, int>{35.0f, 7};
auto b = Containers::pair(35.0f, 7);

Unlike with std::pair, access to the pair elements is done using first() and second() member functions, without providing access to the members directly. This is done in order to future-proof the design and have extra flexibility in how the internals are defined.

There's also a three-element variant, called a Triple.

C++17 structured bindings

If Corrade/Containers/StructuredBindings.h is included, the class can be used in C++17 structured bindings. While the get<i>() overloads are defined inside Pair itself, a separate header is used for the std::tuple_size and std::tuple_element template specializations, as those may require #include <utility> on some STL implementations. Example:

#include <Corrade/Containers/StructuredBindings.h>



auto [first, second] = Containers::pair(42, 3.14f);

STL compatibility

Instances of Pair are implicitly copy- and move-convertible to and from std::pair if you include Corrade/Containers/PairStl.h. The conversion is provided in a separate header to avoid overhead from an unconditional #include <utility>. Additionally, the pair(T&&) overload also allows for such a conversion. Example:

#include <Corrade/Containers/PairStl.h>



std::pair<float, int> a{35.0f, 7};
Containers::Pair<float, int> b{a};

std::pair<bool, int*> c(Containers::pair(false, &b.second()));

auto d = Containers::pair(std::pair<char, double>{'p', 3.14});
    // d is Containers::pair<char, double>

Public types

using FirstType = F
First type.
using SecondType = S
Second type.

Constructors, destructors, conversion operators

Pair(Corrade::DefaultInitT) explicit constexpr noexcept(…)
Construct a default-initialized pair.
Pair(Corrade::ValueInitT) explicit constexpr noexcept(…)
Construct a value-initialized pair.
Pair(Corrade::NoInitT) explicit noexcept(…)
Construct a pair without initializing its contents.
Pair() constexpr noexcept(…)
Default constructor.
Pair(const F& first, const S& second) constexpr noexcept(…)
Constructor.
Pair(const F& first, S&& second) constexpr noexcept(…)
Pair(F&& first, const S& second) constexpr noexcept(…)
Pair(F&& first, S&& second) constexpr noexcept(…)
template<class OtherF, class OtherS, class = typename std::enable_if<std::is_constructible<F, const OtherF&>::value&& std::is_constructible<S, const OtherS&>::value>::type>
Pair(const Pair<OtherF, OtherS>& other) explicit constexpr noexcept(…)
Copy-construct a pair from another of different type.
template<class OtherF, class OtherS, class = typename std::enable_if<std::is_constructible<F, OtherF && >::value&& std::is_constructible<S, OtherS && >::value>::type>
Pair(Pair<OtherF, OtherS>&& other) explicit constexpr noexcept(…)
Move-construct a pair from another of different type.
template<class T, class = decltype(Implementation::PairConverter<F, S, T>::from(std::declval<const T&>()))>
Pair(const T& other) noexcept(…)
Copy-construct a pair from external representation.
template<class T, class = decltype(Implementation::PairConverter<F, S, T>::from(std::declval<T && >()))>
Pair(T&& other) noexcept(…)
Move-construct a pair from external representation.
template<class T, class = decltype(Implementation::PairConverter<F, S, T>::to(std::declval<const Pair<F, S>&>()))>
operator T() const &
Copy-convert the pair to external representation.
template<class T, class = decltype(Implementation::PairConverter<F, S, T>::to(std::declval<Pair<F, S> && >()))>
operator T() &&
Move-convert the pair to external representation.

Public functions

auto operator==(const Pair<F, S>& other) const -> bool constexpr
Equality comparison.
auto operator!=(const Pair<F, S>& other) const -> bool constexpr
Non-equality comparison.
auto first() & -> F& constexpr
First element.
auto first() && -> F constexpr
auto first() const & -> const F& constexpr
auto second() & -> S& constexpr
Second element.
auto second() && -> S constexpr
auto second() const & -> const S& constexpr

Function documentation

template<class F, class S>
Corrade::Containers::Pair<F, S>::Pair(Corrade::DefaultInitT) explicit constexpr noexcept(…)

Construct a default-initialized pair.

Trivial types are not initialized, default constructor called otherwise. Because of the differing behavior for trivial types it's better to explicitly use either the Pair(ValueInitT) or the Pair(NoInitT) variant instead.

template<class F, class S>
Corrade::Containers::Pair<F, S>::Pair(Corrade::ValueInitT) explicit constexpr noexcept(…)

Construct a value-initialized pair.

Trivial types are zero-initialized, default constructor called otherwise. This is the same as the default constructor.

template<class F, class S>
Corrade::Containers::Pair<F, S>::Pair(Corrade::NoInitT) explicit noexcept(…)

Construct a pair without initializing its contents.

Enabled only for trivial types and types that implement the NoInit constructor. The contents are not initialized. Useful if you will be overwriting both members later anyway or if you need to initialize in a way that's not expressible via any other Pair constructor.

For trivial types is equivalent to Pair(DefaultInitT).

template<class F, class S>
Corrade::Containers::Pair<F, S>::Pair() constexpr noexcept(…)

Default constructor.

Alias to Pair(ValueInitT).

template<class F, class S>
Corrade::Containers::Pair<F, S>::Pair(const F& first, const S& second) constexpr noexcept(…)

Constructor.

template<class F, class S>
Corrade::Containers::Pair<F, S>::Pair(const F& first, S&& second) constexpr noexcept(…)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class F, class S>
Corrade::Containers::Pair<F, S>::Pair(F&& first, const S& second) constexpr noexcept(…)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class F, class S>
Corrade::Containers::Pair<F, S>::Pair(F&& first, S&& second) constexpr noexcept(…)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class F, class S> template<class T, class = decltype(Implementation::PairConverter<F, S, T>::from(std::declval<const T&>()))>
Corrade::Containers::Pair<F, S>::Pair(const T& other) noexcept(…)

Copy-construct a pair from external representation.

template<class F, class S> template<class T, class = decltype(Implementation::PairConverter<F, S, T>::from(std::declval<T && >()))>
Corrade::Containers::Pair<F, S>::Pair(T&& other) noexcept(…)

Move-construct a pair from external representation.

template<class F, class S> template<class T, class = decltype(Implementation::PairConverter<F, S, T>::to(std::declval<const Pair<F, S>&>()))>
Corrade::Containers::Pair<F, S>::operator T() const &

Copy-convert the pair to external representation.

template<class F, class S> template<class T, class = decltype(Implementation::PairConverter<F, S, T>::to(std::declval<Pair<F, S> && >()))>
Corrade::Containers::Pair<F, S>::operator T() &&

Move-convert the pair to external representation.

template<class F, class S>
F Corrade::Containers::Pair<F, S>::first() && constexpr

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class F, class S>
const F& Corrade::Containers::Pair<F, S>::first() const & constexpr

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class F, class S>
S Corrade::Containers::Pair<F, S>::second() && constexpr

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class F, class S>
const S& Corrade::Containers::Pair<F, S>::second() const & constexpr

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template<class F, class S> template<class F, class S>
Pair<typename std::decay<F>::type, typename std::decay<S>::type> pair(F&& first, S&& second) constexpr

Make a pair.

Convernience alternative to Pair::Pair(const F&, const S&) and overloads. The following two lines are equivalent:

auto a = Containers::Pair<float, int>{35.0f, 7};
auto b = Containers::pair(35.0f, 7);

template<class F, class S> template<class T>
auto pair(T&& other)

Make a pair from external representation.

template<class F, class S> template<class F, class S>
Utility::Debug& operator<<(Utility::Debug& debug, const Pair<F, S>& value)

Debug output operator.