• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // <type_traits>
12 
13 // constexpr bool is_constant_evaluated() noexcept; // C++20
14 
15 #include <type_traits>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 
20 #ifndef __cpp_lib_is_constant_evaluated
21 #if TEST_HAS_BUILTIN(__builtin_is_constant_evaluated)
22 # error __cpp_lib_is_constant_evaluated should be defined
23 #endif
24 #endif
25 
26 // Disable the tautological constant evaluation warnings for this test,
27 // because it's explicitly testing those cases.
28 #if TEST_HAS_WARNING("-Wconstant-evaluated") && defined(__clang__)
29 #pragma clang diagnostic ignored "-Wconstant-evaluated"
30 #endif
31 
32 template <bool> struct InTemplate {};
33 
main(int,char **)34 int main(int, char**)
35 {
36 #ifdef __cpp_lib_is_constant_evaluated
37 #ifdef TEST_COMPILER_C1XX
38     #pragma warning(push)
39     #pragma warning(disable: 5063) // 'std::is_constant_evaluated' always evaluates to true in manifestly constant-evaluated expressions
40 #endif // TEST_COMPILER_C1XX
41   // Test the signature
42   {
43     ASSERT_SAME_TYPE(decltype(std::is_constant_evaluated()), bool);
44     ASSERT_NOEXCEPT(std::is_constant_evaluated());
45     constexpr bool p = std::is_constant_evaluated();
46     assert(p);
47   }
48   // Test the return value of the builtin for basic sanity only. It's the
49   // compiler's job to test the builtin for correctness.
50   {
51     static_assert(std::is_constant_evaluated(), "");
52     bool p = std::is_constant_evaluated();
53     assert(!p);
54     ASSERT_SAME_TYPE(InTemplate<std::is_constant_evaluated()>, InTemplate<true>);
55     static int local_static = std::is_constant_evaluated() ? 42 : -1;
56     assert(local_static == 42);
57   }
58 #ifdef TEST_COMPILER_C1XX
59     #pragma warning(pop)
60 #endif // TEST_COMPILER_C1XX
61 #endif // __cpp_lib_is_constant_evaluated
62   return 0;
63 }
64