Skip to content

Commit 491ddc0

Browse files
committed
Add a private call '__libcpp_is_constant_evaluated' which 'works' for old language versions and w/o any compiler support. 'Working', in this case, means that it returns false in those cases.
llvm-svn: 364873
1 parent a7f0094 commit 491ddc0

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

libcxx/include/type_traits

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3999,13 +3999,18 @@ enum class endian
39993999
};
40004000
#endif
40014001

4002-
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED)
4002+
#ifndef _LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED
4003+
#if _LIBCPP_STD_VER > 17
40034004
_LIBCPP_INLINE_VISIBILITY
40044005
inline constexpr bool is_constant_evaluated() noexcept {
40054006
return __builtin_is_constant_evaluated();
40064007
}
40074008
#endif
40084009

4010+
_LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT { return __builtin_is_constant_evaluated(); }
4011+
#else
4012+
_LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT { return false; }
4013+
#endif
40094014

40104015
template <class _CharT>
40114016
using _IsCharLikeType = _And<is_standard_layout<_CharT>, is_trivial<_CharT> >;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
10+
// <type_traits>
11+
12+
// __libcpp_is_constant_evaluated()
13+
14+
// returns false when there's no constant evaluation support from the compiler.
15+
// as well as when called not in a constexpr context
16+
17+
#include <type_traits>
18+
#include <cassert>
19+
20+
#include "test_macros.h"
21+
22+
int main (int, char**) {
23+
ASSERT_SAME_TYPE(decltype(std::__libcpp_is_constant_evaluated()), bool);
24+
ASSERT_NOEXCEPT(std::__libcpp_is_constant_evaluated());
25+
26+
#if !defined(_LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED) && !defined(_LIBCPP_CXX03_LANG)
27+
static_assert(std::__libcpp_is_constant_evaluated(), "");
28+
#endif
29+
30+
bool p = std::__libcpp_is_constant_evaluated();
31+
assert(!p);
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)