Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated12.C

raw
1 // PR c++/100995
2 // { dg-do compile { target c++20 } }
3 // { dg-options "-Wtautological-compare" }
4
5 namespace std {
6 constexpr inline bool
7 is_constant_evaluated () noexcept
8 {
9 return __builtin_is_constant_evaluated ();
10 }
11 }
12
13 template <int I = []() constexpr { if (std::is_constant_evaluated()) return 1; return 0; }()>
14 struct X { };
15
16 template <int I = [](){ if (std::is_constant_evaluated()) return 1; return 0; }()>
17 struct Y { };
18
19 template <int I = [](){ if constexpr (std::is_constant_evaluated()) return 1; return 0; }()> // { dg-warning "always evaluates to true" }
20 struct Z { };
21
22 constexpr bool b = true;
23
24 #define __glibcxx_assert(cond) \
25 if (__builtin_is_constant_evaluated() && !bool(cond)) \
26 __builtin_unreachable()
27 #define CHECK __builtin_is_constant_evaluated() // { dg-warning "always evaluates to false" }
28 #define CHECK2 __builtin_is_constant_evaluated()
29
30 int
31 foo ()
32 {
33 if (std::is_constant_evaluated ()) // { dg-warning "always evaluates to false" }
34 return 1;
35 __glibcxx_assert(b);
36 if (CHECK && b)
37 return 2;
38 if (CHECK2)
39 return 3;
40 return 0;
41 }
42
43 constexpr int
44 bar ()
45 {
46 if (std::is_constant_evaluated ())
47 return 1;
48 if constexpr (std::is_constant_evaluated ()) // { dg-warning "always evaluates to true" }
49 return 2;
50 if constexpr (std::is_constant_evaluated () && b) // { dg-warning "always evaluates to true" }
51 return 3;
52 if constexpr (!std::is_constant_evaluated ()) // { dg-warning "always evaluates to true" }
53 return 4;
54 return 0;
55 }
56
57 consteval int
58 baz ()
59 {
60 if (std::is_constant_evaluated ()) // { dg-warning "always evaluates to true" }
61 return 1;
62 return 0;
63 }
64
65 int
66 qux ()
67 {
68 if (({ static bool a = std::is_constant_evaluated (); a; }))
69 return 1;
70 if (({ bool a = std::is_constant_evaluated (); a; }))
71 return 2;
72 if (static bool a = std::is_constant_evaluated (); a)
73 return 3;
74 if (bool a = std::is_constant_evaluated (); a)
75 return 4;
76 if constexpr (constexpr bool a = std::is_constant_evaluated (); a)
77 return 5;
78 return 0;
79 }