Skip to content
Snippets Groups Projects

Add utilities for std::reference_wrapper

Merged Carsten Gräser requested to merge feature/referencehelper into master
3 unresolved threads

This adds the following helpers for handling std::reference_wrapper:

  • A predicate IsReferenceWrapper_v<R> for detecting if R is a std::reference_wrapper<T>.
  • A helper function resolveRef for resolving references wrapped in std::reference_wrapper. For type T providing a method T::foo() this allows to transparently use resolveRef(t).foo() if t is either a plain T or a std::reference_wrapper<T>.
  • A type alias ResolveRef_t<T> to determine the resolved type for T.

Merge request reports

Pipeline #39041 passed

Pipeline passed for 29210d4a on feature/referencehelper

Merged by Carsten GräserCarsten Gräser 3 years ago (Sep 16, 2021 10:01am UTC)

Loading

Pipeline #39393 passed

Pipeline passed for d74194c9 on master

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
43 */
44 template<class T, std::enable_if_t<not IsReferenceWrapper_v<T>, int> = 0>
45 auto& resolveRef(T& gf)
46 {
47 return gf;
48 }
49
50 /**
51 * \brief Helper function to resolve std::reference_wrapper
52 *
53 * This is the overload for plain const l-value reference types.
54 * It simply forwards the passed const l-value reference.
55 *
56 * \ingroup Utility
57 */
58 template<class T, std::enable_if_t<not IsReferenceWrapper_v<T>, int> = 0>
  • Carsten Gräser added 2 commits

    added 2 commits

    • 04df12d8 - Add utilities for std::reference_wrapper
    • 26a304af - [test] Add test for resolveRef and IsReferenceWrapper_v

    Compare with previous version

  • Carsten Gräser added 2 commits

    added 2 commits

    • 94112b1b - Add utilities for std::reference_wrapper
    • 684065fe - [test] Add test for resolveRef and IsReferenceWrapper_v

    Compare with previous version

  • 68 {
    69 return gf.get();
    70 }
    71
    72
    73 /**
    74 * \brief Helper function to resolve std::reference_wrapper
    75 *
    76 * This is the overload for const std::reference_wrapper<T>.
    77 * It resolves the reference by returning the stored
    78 * (mutable or const) l-value reference.
    79 *
    80 * \ingroup Utility
    81 */
    82 template<class T>
    83 auto& resolveRef(const std::reference_wrapper<T>& gf)
    • One could even go one step further an replace all the overloads with different const/ref qualifiern of std::reference_wrapper with just one function without reference:

      template<class T>
      auto& resolveRef(std::reference_wrapper<T> gf)
      {
        return gf.get();
      }

      It has precedence over the other non-reference_wrapper overloads and handles all three cases at once. The reference_wrapper can be copied without copying the underlying wrapped element.

    • changed this line in version 5 of the diff

    • You're right, and the copy should not harm since std::reference_wrapper is trivially copyable. I implemented the change. Notice that the deleted overload for T&& is also not needed but is deliberately kept for documentation purpose and to get nicer compile errors. I also added constexpr and noexcept in the new version.

    • Nice.

      I also added constexpr and noexcept in the new version.

      Unfortunately, std::reference_wrapper::get is marked constexpr only since c++20. So, one cannot use the utility in constexpr-contexts pre-c++20. But this should not be problematic.

    • Please register or sign in to reply
  • Carsten Gräser added 2 commits

    added 2 commits

    • e66acc60 - Add utilities for std::reference_wrapper
    • 29210d4a - [test] Add test for resolveRef and IsReferenceWrapper_v

    Compare with previous version

  • No objection, merging now. If this turns out to be useful we may move it to dune-common later.

  • Carsten Gräser mentioned in commit d74194c9

    mentioned in commit d74194c9

  • mentioned in merge request core/dune-common!1100 (merged)

  • Please register or sign in to reply
    Loading