Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
as_const.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_AS_CONST_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_AS_CONST_HPP_
7
8// IWYU pragma: private, include "arene/base/utility.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11#include "arene/base/stdlib_choice/add_const.hpp"
12
13namespace arene {
14namespace base {
15
16// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
17// parasoft-begin-suppress AUTOSAR-A7_5_1-a-2 "Temporary objects are handled by a different overload of this function"
18/// @brief Obtain an lvalue-reference to a const object of type T.
19///
20/// Use this as an easier way to obtain a const-reference, deducing the appropriate type compared to writing explicit
21/// const_cast expressions.
22///
23/// @note This is a back-port of the @c std::as_const() function.
24///
25/// @tparam T The type of the object to add @c const to
26/// @param obj The object to obtain a const-reference to.
27/// @return A const-lvalue-reference to the argument.
28template <typename T>
29constexpr auto as_const(T& obj) noexcept -> std::add_const_t<T>& {
30 return obj;
31}
32// parasoft-end-suppress AUTOSAR-A7_5_1-a-2
33// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
34
35// parasoft-begin-suppress AUTOSAR-M3_3_2 "False positive: inline function used in multiple translation units"
36// parasoft-begin-suppress AUTOSAR-A8_4_12 "False positive: T is not deduced as a reference"
37/// @brief Prevent passing rvalue arguments to as_const().
38/// @tparam T The type of the object to add @c const to
39/// @param rvalue The rvalue object
40template <typename T>
41constexpr void as_const(T const&& rvalue) = delete;
42// parasoft-end-suppress AUTOSAR-A8_4_12
43// parasoft-end-suppress AUTOSAR-M3_3_2
44
45} // namespace base
46} // namespace arene
47
48#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_AS_CONST_HPP_
Definition array_exceptions_disabled.cpp:11
constexpr void as_const(T const &&rvalue)=delete
Prevent passing rvalue arguments to as_const().
constexpr auto as_const(T &obj) noexcept -> std::add_const_t< T > &
Obtain an lvalue-reference to a const object of type T.
Definition as_const.hpp:29
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10