template<class T>
Corrade::Containers::Reference class

Lightweight non-owning reference wrapper.

Equivalent to std::reference_wrapper from C++11, provides a copyable non-owning wrapper over l-value references to allow storing them in containers. The reference() convenience function provides an equivalent to std::ref(), see its documentation for a potential use case. An equivalent to std::cref() is not deemed necessary — in most contexts where Reference is used, passing a plain reference works just as well, with no need to explicitly wrap it first. This class is trivially copyable (std::reference_wrapper is guaranteed to be so only since C++17) and also works on incomplete types, which std::reference_wrapper knows since C++20.

This class is exclusively for l-value references. If you want to accept r-value references instead, use a MoveReference; if you want to accept both, use an AnyReference.

STL compatibility

Instances of Reference are implicitly convertible to and from std::reference_wrapper if you include Corrade/Containers/ReferenceStl.h. The conversion is provided in a separate header to avoid unconditional #include <functional>, which significantly affects compile times. Example:

#include <Corrade/Containers/ReferenceStl.h>



int a = 1337;
Containers::Reference<int> b = a;

std::reference_wrapper<int> c = b;
Containers::Reference<const int> d = std::cref(a);

Public types

using Type = T new in Git master
Value type.

Constructors, destructors, conversion operators

Reference(T& reference) constexpr noexcept
Constructor.
template<class U, class = decltype(Implementation::ReferenceConverter<T, U>::from(std::declval<U>()))>
Reference(U other) constexpr noexcept
Construct a reference from external representation.
Reference(T&&) deleted
Construction from r-value references is not allowed.
template<class U, class = typename std::enable_if<std::is_base_of<T, U>::value>::type>
Reference(Reference<U> other) constexpr noexcept
Construct a reference from another of a derived type.
template<class U, class = decltype(Implementation::ReferenceConverter<T, U>::to(std::declval<Reference<T>>()))>
operator U() const constexpr
Convert the reference to external representation.
operator T&() const constexpr
Underlying reference.
operator Reference<const T>() const constexpr

Public functions

auto get() const -> T& constexpr
Underlying reference.
auto operator->() const -> T* constexpr
Access the underlying reference.
auto operator*() const -> T& constexpr
Access the underlying reference.

Function documentation

template<class T> template<class U, class = decltype(Implementation::ReferenceConverter<T, U>::from(std::declval<U>()))>
Corrade::Containers::Reference<T>::Reference(U other) constexpr noexcept

Construct a reference from external representation.

template<class T>
Corrade::Containers::Reference<T>::Reference(T&&) deleted

Construction from r-value references is not allowed.

A MoveReference can be created from r-value references instead.

template<class T> template<class U, class = typename std::enable_if<std::is_base_of<T, U>::value>::type>
Corrade::Containers::Reference<T>::Reference(Reference<U> other) constexpr noexcept

Construct a reference from another of a derived type.

Expects that T is a base of U.

template<class T> template<class U, class = decltype(Implementation::ReferenceConverter<T, U>::to(std::declval<Reference<T>>()))>
Corrade::Containers::Reference<T>::operator U() const constexpr

Convert the reference to external representation.

template<class T>
Corrade::Containers::Reference<T>::operator Reference<const T>() 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 T>
T* Corrade::Containers::Reference<T>::operator->() const constexpr

Access the underlying reference.

get(), operator*()

template<class T>
T& Corrade::Containers::Reference<T>::operator*() const constexpr

Access the underlying reference.

template<class T> template<class T>
Reference<T> reference(T& reference) constexpr new in Git master

Make a reference wrapper.

Convenience alternative to Reference::Reference(T&). Thw following two lines are equivalent:

float target = ;

auto a = Containers::Reference<float>{target};
auto b = Containers::reference(target);

Useful for example when iterating a list of variables that need to be modified in-place, and where the code would otherwise have to be extra verbose or would fall back to raw pointers and risk them getting nullptr:

for(int& i: {Containers::reference(a),
             Containers::reference(b),
             Containers::reference(c)})
    i = i*i;

template<class T> template<class T>
Utility::Debug& operator<<(Utility::Debug& debug, Reference<T> value)

Debug output operator.