Skip to content

Commit 05731c2

Browse files
Add etl::is_constant_evaluated (#1198)
Co-authored-by: John Wellbelove <[email protected]>
1 parent 85fc483 commit 05731c2

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

include/etl/profiles/determine_builtin_support.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ SOFTWARE.
5555
#if !defined(ETL_USING_BUILTIN_UNDERLYING_TYPE)
5656
#define ETL_USING_BUILTIN_UNDERLYING_TYPE 1
5757
#endif
58+
59+
#if !defined(ETL_USING_BUILTIN_IS_CONSTANT_EVALUATED)
60+
#define ETL_USING_BUILTIN_IS_CONSTANT_EVALUATED 1
61+
#endif
5862
#endif
5963

6064
#if defined(__has_builtin) && !defined(ETL_COMPILER_MICROSOFT) // Use __has_builtin to check for existence of builtin functions? Fix VS2022 intellisense issue.
@@ -82,6 +86,10 @@ SOFTWARE.
8286
#define ETL_USING_BUILTIN_UNDERLYING_TYPE __has_builtin(__underlying_type)
8387
#endif
8488

89+
#if !defined(ETL_USING_BUILTIN_IS_CONSTANT_EVALUATED)
90+
#define ETL_USING_BUILTIN_IS_CONSTANT_EVALUATED __has_builtin(__builtin_is_constant_evaluated)
91+
#endif
92+
8593
#if !defined(ETL_USING_BUILTIN_MEMCPY)
8694
#define ETL_USING_BUILTIN_MEMCPY __has_builtin(__builtin_memcpy)
8795
#endif
@@ -128,6 +136,10 @@ SOFTWARE.
128136
#define ETL_USING_BUILTIN_UNDERLYING_TYPE 0
129137
#endif
130138

139+
#if !defined(ETL_USING_BUILTIN_IS_CONSTANT_EVALUATED)
140+
#define ETL_USING_BUILTIN_IS_CONSTANT_EVALUATED 0
141+
#endif
142+
131143
#if !defined(ETL_USING_BUILTIN_MEMCPY)
132144
#define ETL_USING_BUILTIN_MEMCPY 0
133145
#endif
@@ -160,6 +172,7 @@ namespace etl
160172
static ETL_CONSTANT bool using_builtin_is_trivially_destructible = (ETL_USING_BUILTIN_IS_TRIVIALLY_DESTRUCTIBLE == 1);
161173
static ETL_CONSTANT bool using_builtin_is_trivially_copyable = (ETL_USING_BUILTIN_IS_TRIVIALLY_COPYABLE == 1);
162174
static ETL_CONSTANT bool using_builtin_underlying_type = (ETL_USING_BUILTIN_UNDERLYING_TYPE == 1);
175+
static ETL_CONSTANT bool using_builtin_is_constant_evaluated = (ETL_USING_BUILTIN_IS_CONSTANT_EVALUATED == 1);
163176
static ETL_CONSTANT bool using_builtin_memcpy = (ETL_USING_BUILTIN_MEMCPY == 1);
164177
static ETL_CONSTANT bool using_builtin_memmove = (ETL_USING_BUILTIN_MEMMOVE == 1);
165178
static ETL_CONSTANT bool using_builtin_memset = (ETL_USING_BUILTIN_MEMSET == 1);

include/etl/type_traits.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,6 +2482,28 @@ typedef integral_constant<bool, true> true_type;
24822482
template <typename T, template <typename...> class Template>
24832483
inline constexpr bool is_specialization_v = etl::is_specialization<T, Template>::value;
24842484
#endif
2485+
2486+
//*********************************************
2487+
// is_constant_evaluated
2488+
ETL_CONSTEXPR inline bool is_constant_evaluated() ETL_NOEXCEPT
2489+
{
2490+
#if ETL_USING_CPP23
2491+
if consteval
2492+
{
2493+
return true;
2494+
}
2495+
else
2496+
{
2497+
return false;
2498+
}
2499+
#elif ETL_USING_BUILTIN_IS_CONSTANT_EVALUATED == 1
2500+
// fallback for C++20 on supported compilers
2501+
return __builtin_is_constant_evaluated();
2502+
#else
2503+
// default if unsupported
2504+
return false;
2505+
#endif
2506+
}
24852507
}
24862508

24872509
// Helper macros

test/test_type_traits.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,4 +1472,22 @@ namespace
14721472
CHECK_FALSE((etl::is_specialization<other_specialized<int>, specialized>::value));
14731473
#endif
14741474
}
1475+
1476+
#if ETL_USING_CPP11
1477+
//*************************************************************************
1478+
TEST(test_is_constant_evaluated)
1479+
{
1480+
const bool c0 = etl::is_constant_evaluated();
1481+
#if !ETL_USING_CPP23 && defined(ETL_COMPILER_MICROSOFT)
1482+
// Not supported on MSVC via __has_builtin, see determine_builtin_support.h
1483+
CHECK_FALSE(c0);
1484+
#else
1485+
CHECK_TRUE(c0);
1486+
#endif
1487+
1488+
volatile int i = 1;
1489+
const bool c1 = (((i == 1) && etl::is_constant_evaluated()) ? true : false);
1490+
CHECK_FALSE(c1);
1491+
}
1492+
#endif
14751493
}

0 commit comments

Comments
 (0)