Dune Core Modules (unstable)

referencehelper.hh
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 © 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
13namespace Dune {
14
15namespace 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
34template<class T>
35constexpr bool IsReferenceWrapper_v = Impl::IsReferenceWrapper<T>::value;
36
37
46template<class T>
47constexpr 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.
59template<class T>
60const auto& resolveRef(T&& gf) = delete;
61
62
81template<class T>
82constexpr T& resolveRef(std::reference_wrapper<T> gf) noexcept
83{
84 return gf.get();
85}
86
87
88
99template<class T>
100using 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 bool IsReferenceWrapper_v
Helper to detect if given type is a std::reference_wrapper.
Definition: referencehelper.hh:35
constexpr T & resolveRef(T &gf) noexcept
Helper function to resolve std::reference_wrapper.
Definition: referencehelper.hh:47
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
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)