]> 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
fbd26352 1// Copyright (C) 2018-2019 Free Software Foundation, Inc.
0eed6e63 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
24template<int N>
25struct X { int v = N; };
26X<std::is_constant_evaluated()> x; // type X<true>
27int y = 4;
28int a = std::is_constant_evaluated() ? y : 1; // initializes a to 1
29int b = std::is_constant_evaluated() ? 2 : y; // initializes b to 2
30int c = y + (std::is_constant_evaluated() ? 2 : y); // initializes c to 2*y
31int d = std::is_constant_evaluated(); // initializes d to 1
32int e = d + std::is_constant_evaluated(); // initializes e to 1 + 0
33
34constexpr int
35foo(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
43constexpr int
44bar()
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
54int p = foo(0); // m == 13; initialized to 26
55int q = p + foo(0); // m == 17 for this call; initialized to 56
56static_assert(bar() == 26, "bar");
57
58struct S { int a, b; };
59
60S s = { std::is_constant_evaluated() ? 2 : 3, y };
61S t = { std::is_constant_evaluated() ? 2 : 3, 4 };
62
63static_assert(std::is_same<decltype(x), X<true> >::value, "x's type");
64
65void
66test01()
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
77int main()
78{
79 test01();
80}