summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated9.C
blob: 378336989922b89c8546a29ad2be6ed5e9f43dc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// PR c++/91428 - warn about std::is_constant_evaluated in if constexpr.
// { dg-do compile { target c++2a } }
// { dg-options "-Wtautological-compare" }

namespace std {
  constexpr inline bool
  is_constant_evaluated () noexcept
  {
    return __builtin_is_constant_evaluated (); 
  }
}

constexpr int
foo(int i)
{
  if constexpr (std::is_constant_evaluated ()) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." }
    return 42;
  else
    return i;
}

constexpr int
foo2(int i)
{
  if constexpr (__builtin_is_constant_evaluated ()) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." }
    return 42;
  else
    return i;
}

constexpr int
foo3(int i)
{
  // I is not a constant expression but we short-circuit it.
  if constexpr (__builtin_is_constant_evaluated () || i)
    return 42;
  else
    return i;
}

constexpr int
foo4(int i)
{
  const int j = 0;
  if constexpr (j && __builtin_is_constant_evaluated ())
    return 42;
  else
    return i;
}