Debian Codegraph @

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

raw
1 // PR c++/91428 - warn about std::is_constant_evaluated in if constexpr.
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 constexpr int
14 foo(int i)
15 {
16 if constexpr (std::is_constant_evaluated ()) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." }
17 return 42;
18 else
19 return i;
20 }
21
22 constexpr int
23 foo2(int i)
24 {
25 if constexpr (__builtin_is_constant_evaluated ()) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." }
26 return 42;
27 else
28 return i;
29 }
30
31 constexpr int
32 foo3(int i)
33 {
34 // I is not a constant expression but we short-circuit it.
35 if constexpr (__builtin_is_constant_evaluated () || i) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." }
36 return 42;
37 else
38 return i;
39 }
40
41 constexpr int
42 foo4(int i)
43 {
44 const int j = 0;
45 if constexpr (j && __builtin_is_constant_evaluated ()) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." }
46 return 42;
47 else
48 return i;
49 }