Skip to content
Snippets Groups Projects

Add utilities for working with `std::reference_wrapper`

Merged Carsten Gräser requested to merge feature/referencehelper into master
1 unresolved thread

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 if T is an instantiation of std::reference_wrapper
  • The function resolveRef(t) either returns t.get() or t depending on whether t is a std::reference_wrapper or a plain l-value reference.
  • ResolveRef_t<T> provides the resolved type for T.

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();

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • added 1 commit

    • da4bd702 - Add utilities for working with `std::reference_wrapper`

    Compare with previous version

  • Carsten Gräser resolved all threads

    resolved all threads

  • Carsten Gräser resolved all threads

    resolved all threads

  • mentioned in merge request dune-grid!577 (merged)

  • mentioned in commit fc637911

  • requested review from @simon.praetorius

  • Please register or sign in to reply
    Loading