dune-common  2.9.0
referencehelper.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_COMMON_REFERENCE_HELPER_HH
6 #define DUNE_COMMON_REFERENCE_HELPER_HH
7 
8 #include <type_traits>
9 #include <functional>
10 
11 
12 
13 namespace Dune {
14 
15 namespace Impl {
16 
17  template<class T>
18  class IsReferenceWrapper : public std::false_type {};
19 
20  template<class T>
21  class IsReferenceWrapper<std::reference_wrapper<T>> : public std::true_type {};
22 
23  template<class T>
24  class IsReferenceWrapper<const std::reference_wrapper<T>> : public std::true_type {};
25 
26 } // namespace Dune::Impl
27 
28 
34 template<class T>
35 constexpr bool IsReferenceWrapper_v = Impl::IsReferenceWrapper<T>::value;
36 
37 
46 template<class T>
47 constexpr T& resolveRef(T& gf) noexcept
48 {
49  return gf;
50 }
51 
52 
53 // There's no overload for non std::reference_wrapper r-values,
54 // because this may lead to undefined behavior whenever the
55 // return value is stored.
56 // Notice that deleting the overload is not necessary, but
57 // helps to document that it is missing on purpose. It also
58 // leads to nicer error messages.
59 template<class T>
60 const auto& resolveRef(T&& gf) = delete;
61 
62 
81 template<class T>
82 constexpr T& resolveRef(std::reference_wrapper<T> gf) noexcept
83 {
84  return gf.get();
85 }
86 
87 
88 
99 template<class T>
100 using ResolveRef_t = std::remove_reference_t<decltype(Dune::resolveRef(std::declval<T&>()))>;
101 
102 
103 } // namespace Dune
104 
105 
106 
107 #endif // DUNE_COMMON_REFERENCE_HELPER_HH
constexpr T & resolveRef(T &gf) noexcept
Helper function to resolve std::reference_wrapper.
Definition: referencehelper.hh:47
constexpr bool IsReferenceWrapper_v
Helper to detect if given type is a std::reference_wrapper.
Definition: referencehelper.hh:35
Dune namespace.
Definition: alignedallocator.hh:13
std::remove_reference_t< decltype(Dune::resolveRef(std::declval< T & >()))> ResolveRef_t
Type trait to resolve std::reference_wrapper.
Definition: referencehelper.hh:100