]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/is_constant_evaluated/1.cc
1 // Copyright (C) 2018-2022 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
21 #include <type_traits>
22 #include <testsuite_hooks.h>
25 struct X
{ int v
= N
; };
26 X
<std::is_constant_evaluated()> x
; // type X<true>
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
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
;
46 const int n
= std::is_constant_evaluated() ? 13 : 17;
48 X
<std::is_constant_evaluated() ? 13 : 17> x2
;
49 static_assert(std::is_same
<decltype(x1
), decltype(x2
)>::value
,
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");
58 struct S
{ int a
, b
; };
60 S s
= { std::is_constant_evaluated() ? 2 : 3, y
};
61 S t
= { std::is_constant_evaluated() ? 2 : 3, 4 };
63 static_assert(std::is_same
<decltype(x
), X
<true> >::value
, "x's type");
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)
73 constexpr int w
= foo (0);