Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
is_possibly_constant_evaluated.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_COMPILER_SUPPORT_IS_POSSIBLY_CONSTANT_EVALUATED_HPP_
6
#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPILER_SUPPORT_IS_POSSIBLY_CONSTANT_EVALUATED_HPP_
7
8
#include "
arene/base/compiler_support/platform_queries/has_builtin_is_constant_evaluated.hpp
"
9
#include "
arene/base/compiler_support/preprocessor/define_configuration.hpp
"
10
// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
11
12
// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
13
// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
14
15
// NOLINTBEGIN(cppcoreguidelines-macro-usage)
16
// parasoft-begin-suppress AUTOSAR-A16_0_1-a-2 "Conditional define permitted by A16-0-1 Permit #2"
17
// parasoft-begin-suppress AUTOSAR-A16_0_1-d-2 "Conditional define permitted by A16-0-1 Permit #2"
18
19
#if ARENE_IS_ON(ARENE_HAS_BUILTIN_IS_CONSTANT_EVALUATED)
20
// Call into the compiler intrinsic to detect this.
21
#define ARENE_IS_POSSIBLY_CONSTANT_EVALUATED_RETURN_EXPRESSION __builtin_is_constant_evaluated()
22
#else
23
// We can't detect if we are in a constant evaluation context so
24
// conservatively return yes (since it's "possible" we are).
25
#define ARENE_IS_POSSIBLY_CONSTANT_EVALUATED_RETURN_EXPRESSION true
26
#endif
27
28
namespace
arene
{
29
namespace
base {
30
31
/// @brief Query to determine if the caller may be in a constant-evaluated-context.
32
/// @return true if the call _may_ be in a constant-evaluated context.
33
/// @return false If the call is manifestly not in a constant-evaluated context.
34
///
35
/// This trait can be used to provide both constexpr and non-constexpr specializations of an implementation when there
36
/// is a performance optimization that can only be performed in non-contexpr contexts.
37
///
38
/// @note This API is similar, but not identical to,
39
/// C++20's @c [std::is_constant_evaluated](https://en.cppreference.com/w/cpp/types/is_constant_evaluated) .
40
/// Determining if the current execution context is constant-evaluated requires compiler support through an intrinsic
41
/// added specifically to allow the implementation of @c std::is_constant_evaluated . Therefore on compilers lacking
42
/// these intrinsics, it is not possible to determine if the current context is constant-evaluated. As such, the name
43
/// has been changed to reflect the loosened semantics: while @c is_constant_evaluated returns @c true IFF the current
44
/// context is a constant evaluated context, @c is_possibly_constant_evaluated returns @c false IFF the current
45
/// context is **not** a constant evaluated context. Relaxing this constraint allows code to be written in pre-C++20
46
/// contexts to perform this query, and if the compiler version supports it, benefit, while always safely falling back
47
/// to the @c constexpr compatible implementation when compiled with compilers which do not support the intrinsic.
48
constexpr
auto
is_possibly_constant_evaluated
() noexcept ->
bool
{
49
return
ARENE_IS_POSSIBLY_CONSTANT_EVALUATED_RETURN_EXPRESSION
;
50
}
51
}
// namespace base
52
}
// namespace arene
53
54
// NOLINTEND(cppcoreguidelines-macro-usage)
55
// parasoft-end-suppress AUTOSAR-A16_0_1-a-2
56
// parasoft-end-suppress AUTOSAR-A16_0_1-d-2
57
58
#endif
// INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPILER_SUPPORT_IS_POSSIBLY_CONSTANT_EVALUATED_HPP_
define_configuration.hpp
has_builtin_is_constant_evaluated.hpp
ARENE_IS_POSSIBLY_CONSTANT_EVALUATED_RETURN_EXPRESSION
#define ARENE_IS_POSSIBLY_CONSTANT_EVALUATED_RETURN_EXPRESSION
Definition
is_possibly_constant_evaluated.hpp:25
arene::base::is_possibly_constant_evaluated
constexpr auto is_possibly_constant_evaluated() noexcept -> bool
Query to determine if the caller may be in a constant-evaluated-context.
Definition
is_possibly_constant_evaluated.hpp:48
arene
Definition
accumulate.hpp:36
arene
base
compiler_support
is_possibly_constant_evaluated.hpp
Generated by
1.13.2