GCC Code Coverage Report


Directory: gridformat/
File: gridformat/common/lvalue_reference.hpp
Date: 2024-11-20 14:41:59
Exec Total Coverage
Lines: 3 3 100.0%
Functions: 133 165 80.6%
Branches: 0 0 -%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2 // SPDX-License-Identifier: MIT
3 /*!
4 * \file
5 * \ingroup Common
6 * \copydoc GridFormat::LValueReferenceOf
7 */
8 #ifndef GRIDFORMAT_COMMON_LVALUE_REFERENCE_HPP_
9 #define GRIDFORMAT_COMMON_LVALUE_REFERENCE_HPP_
10
11 #include <type_traits>
12 #include <concepts>
13
14 namespace GridFormat {
15
16 /*!
17 * \ingroup Common
18 * \brief Helper class that can be used in interfaces that require lvalue references.
19 * \details Since temporaries of T also bind to const T&, interfaces (e.g. constructors)
20 * that take a reference and store it can lead to dangling references when called
21 * with temporaries. In those places, using this helper type yields compile-time
22 * errors in such cases.
23 */
24 template<typename T>
25 class LValueReferenceOf {
26 public:
27 template<typename _T>
28 requires(std::same_as<std::remove_const_t<T>, std::remove_cvref_t<_T>>)
29 83320 LValueReferenceOf(_T&& ref) : _ref{ref} {
30 static_assert(
31 std::is_lvalue_reference_v<_T>,
32 "Cannot bind a temporary to an lvalue reference."
33 );
34 83320 }
35
36 83312 T& get() { return _ref; }
37 std::add_const_t<T>& get() const { return _ref; }
38
39 private:
40 T& _ref;
41 };
42
43 template<typename T>
44 LValueReferenceOf(T&&) -> LValueReferenceOf<std::remove_reference_t<T>>;
45
46 } // namespace GridFormat
47
48 #endif // GRIDFORMAT_COMMON_LVALUE_REFERENCE_HPP_
49