]> git.ipfire.org Git - thirdparty/gcc.git/blob - 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
1 // Copyright (C) 2018-2023 Free Software Foundation, Inc.
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
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20
21 #include <type_traits>
22 #include <testsuite_hooks.h>
23
24 template<int N>
25 struct X { int v = N; };
26 X<std::is_constant_evaluated()> x; // type X<true>
27 int y = 4;
28 int a = std::is_constant_evaluated() ? y : 1; // initializes a to 1
29 int b = std::is_constant_evaluated() ? 2 : y; // initializes b to 2
30 int c = y + (std::is_constant_evaluated() ? 2 : y); // initializes c to 2*y
31 int d = std::is_constant_evaluated(); // initializes d to 1
32 int e = d + std::is_constant_evaluated(); // initializes e to 1 + 0
33
34 constexpr int
35 foo(int x)
36 {
37 const int n = std::is_constant_evaluated() ? 13 : 17; // n == 13
38 int m = std::is_constant_evaluated() ? 13 : 17; // m might be 13 or 17 (see below)
39 char arr[n] = {}; // char[13]
40 return m + sizeof (arr) + x;
41 }
42
43 constexpr int
44 bar()
45 {
46 const int n = std::is_constant_evaluated() ? 13 : 17;
47 X<n> x1;
48 X<std::is_constant_evaluated() ? 13 : 17> x2;
49 static_assert(std::is_same<decltype(x1), decltype(x2)>::value,
50 "x1/x2's type");
51 return x1.v + x2.v;
52 }
53
54 int p = foo(0); // m == 13; initialized to 26
55 int q = p + foo(0); // m == 17 for this call; initialized to 56
56 static_assert(bar() == 26, "bar");
57
58 struct S { int a, b; };
59
60 S s = { std::is_constant_evaluated() ? 2 : 3, y };
61 S t = { std::is_constant_evaluated() ? 2 : 3, 4 };
62
63 static_assert(std::is_same<decltype(x), X<true> >::value, "x's type");
64
65 void
66 test01()
67 {
68 VERIFY( a == 1 && b == 2 && c == 8 && d == 1 && e == 1 && p == 26 );
69 VERIFY( q == 56 && s.a == 3 && s.b == 4 && t.a == 2 && t.b == 4 );
70 VERIFY( foo (y) == 34 );
71 if constexpr (foo (0) != 26)
72 VERIFY( 0 );
73 constexpr int w = foo (0);
74 VERIFY( w == 26 );
75 }
76
77 int main()
78 {
79 test01();
80 }