Add utilities for working with `std::reference_wrapper`
This adds three utilities (stolen from staging/dune-functions!315 (merged)) and a
corresponding test for working with std::reference_wrapper
:
-
IsReferenceWrapper_v<T>
allows to check ifT
is an instantiation ofstd::reference_wrapper
- The function
resolveRef(t)
either returnst.get()
ort
depending on whethert
is astd::reference_wrapper
or a plain l-value reference. -
ResolveRef_t<T>
provides the resolved type forT
.
These utilities allow to support storing data members of classes by value or
reference transparently suing the following pattern. The class always stores
values, but supports passing std::ref(...)
to opt-in store-by-reference.
Handling the latter can be done without boilerplate code using the
provided utilities:
template<class StoredT>
struct SomeWrapper {
using T = ResolveRef_t<StoredT>;
SomeWrapper(const StoredT& t) : t_(t) {}
void callFoo() const {
resolveRef(t_).foo();
}
StoredT t_;
};
[...]
// Store t by value
auto w1 = SomeWrapper(t);
w1.callFoo();
// Store t by reference
auto w2 = SomeWrapper(std::cref(t));
w2.callFoo();