]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/is_constant_evaluated/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / is_constant_evaluated / 1.cc
CommitLineData
a945c346 1// Copyright (C) 2018-2024 Free Software Foundation, Inc.
0d7924f2
JJ
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
6d0b43f5 18// { dg-do run { target c++20 } }
0d7924f2
JJ
19
20#include <type_traits>
21#include <testsuite_hooks.h>
22
23template<int N>
24struct X { int v = N; };
25X<std::is_constant_evaluated()> x; // type X<true>
26int y = 4;
27int a = std::is_constant_evaluated() ? y : 1; // initializes a to 1
28int b = std::is_constant_evaluated() ? 2 : y; // initializes b to 2
29int c = y + (std::is_constant_evaluated() ? 2 : y); // initializes c to 2*y
30int d = std::is_constant_evaluated(); // initializes d to 1
31int e = d + std::is_constant_evaluated(); // initializes e to 1 + 0
32
33constexpr int
34foo(int x)
35{
36 const int n = std::is_constant_evaluated() ? 13 : 17; // n == 13
37 int m = std::is_constant_evaluated() ? 13 : 17; // m might be 13 or 17 (see below)
38 char arr[n] = {}; // char[13]
39 return m + sizeof (arr) + x;
40}
41
42constexpr int
43bar()
44{
45 const int n = std::is_constant_evaluated() ? 13 : 17;
46 X<n> x1;
47 X<std::is_constant_evaluated() ? 13 : 17> x2;
48 static_assert(std::is_same<decltype(x1), decltype(x2)>::value,
49 "x1/x2's type");
50 return x1.v + x2.v;
51}
52
53int p = foo(0); // m == 13; initialized to 26
54int q = p + foo(0); // m == 17 for this call; initialized to 56
55static_assert(bar() == 26, "bar");
56
57struct S { int a, b; };
58
59S s = { std::is_constant_evaluated() ? 2 : 3, y };
60S t = { std::is_constant_evaluated() ? 2 : 3, 4 };
61
62static_assert(std::is_same<decltype(x), X<true> >::value, "x's type");
63
64void
65test01()
66{
67 VERIFY( a == 1 && b == 2 && c == 8 && d == 1 && e == 1 && p == 26 );
68 VERIFY( q == 56 && s.a == 3 && s.b == 4 && t.a == 2 && t.b == 4 );
69 VERIFY( foo (y) == 34 );
70 if constexpr (foo (0) != 26)
71 VERIFY( 0 );
72 constexpr int w = foo (0);
73 VERIFY( w == 26 );
74}
75
76int main()
77{
78 test01();
79}