summaryrefslogtreecommitdiff
path: root/libstdc++-v3
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2018-12-12 09:31:01 +0100
committerJakub Jelinek <jakub@gcc.gnu.org>2018-12-12 09:31:01 +0100
commit0d7924f2e766834e4fec84ff093f4c355fea6be1 (patch)
tree93795a415d66bf514bcedfb5b91ef21766532313 /libstdc++-v3
parentcce3ae91f21f185756f604f529501810d8025f37 (diff)
downloadgcc-0d7924f2e766834e4fec84ff093f4c355fea6be1.tar.gz
P0595R2 - is_constant_evaluated
P0595R2 - is_constant_evaluated * include/bits/c++config (_GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED): Define if __builtin_is_constant_evaluated is available. * include/std/type_traits (std::is_constant_evaluated): New constexpr inline function. * testsuite/20_util/is_constant_evaluated/1.cc: New test. * testsuite/20_util/is_constant_evaluated/noexcept.cc: New test. From-SVN: r267045
Diffstat (limited to 'libstdc++-v3')
-rw-r--r--libstdc++-v3/ChangeLog10
-rw-r--r--libstdc++-v3/include/bits/c++config6
-rw-r--r--libstdc++-v3/include/std/type_traits6
-rw-r--r--libstdc++-v3/testsuite/20_util/is_constant_evaluated/1.cc80
-rw-r--r--libstdc++-v3/testsuite/20_util/is_constant_evaluated/noexcept.cc23
5 files changed, 125 insertions, 0 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 75ca6098da6..81dc78be260 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,13 @@
+2018-12-12 Jakub Jelinek <jakub@redhat.com>
+
+ P0595R2 - is_constant_evaluated
+ * include/bits/c++config (_GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED):
+ Define if __builtin_is_constant_evaluated is available.
+ * include/std/type_traits (std::is_constant_evaluated): New constexpr
+ inline function.
+ * testsuite/20_util/is_constant_evaluated/1.cc: New test.
+ * testsuite/20_util/is_constant_evaluated/noexcept.cc: New test.
+
2018-12-10 Gerald Pfeifer <gerald@pfeifer.com>
* doc/xml/manual/documentation_hacking.xml: Update reference
diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config
index d499d32b51e..bab2bda34ef 100644
--- a/libstdc++-v3/include/bits/c++config
+++ b/libstdc++-v3/include/bits/c++config
@@ -627,6 +627,9 @@ namespace std
# define _GLIBCXX_HAVE_BUILTIN_HAS_UNIQ_OBJ_REP 1
# define _GLIBCXX_HAVE_BUILTIN_IS_AGGREGATE 1
# define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1
+# if __GNUC__ >= 9
+# define _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED 1
+# endif
#elif defined(__is_identifier)
// For non-GNU compilers:
# if ! __is_identifier(__has_unique_object_representations)
@@ -638,6 +641,9 @@ namespace std
# if ! __is_identifier(__builtin_launder)
# define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1
# endif
+# if ! __is_identifier(__builtin_is_constant_evaluated)
+# define _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED 1
+# endif
#endif // GCC
// End of prewritten config; the settings discovered at configure time follow.
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 727a5451c56..2171d13bf3b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3029,6 +3029,12 @@ template <typename _From, typename _To>
template<typename _Tp>
using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
+#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
+ constexpr inline bool
+ is_constant_evaluated() noexcept
+ { return __builtin_is_constant_evaluated(); }
+#endif
+
#endif // C++2a
_GLIBCXX_END_NAMESPACE_VERSION
diff --git a/libstdc++-v3/testsuite/20_util/is_constant_evaluated/1.cc b/libstdc++-v3/testsuite/20_util/is_constant_evaluated/1.cc
new file mode 100644
index 00000000000..c72574ead47
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_constant_evaluated/1.cc
@@ -0,0 +1,80 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <type_traits>
+#include <testsuite_hooks.h>
+
+template<int N>
+struct X { int v = N; };
+X<std::is_constant_evaluated()> x; // type X<true>
+int y = 4;
+int a = std::is_constant_evaluated() ? y : 1; // initializes a to 1
+int b = std::is_constant_evaluated() ? 2 : y; // initializes b to 2
+int c = y + (std::is_constant_evaluated() ? 2 : y); // initializes c to 2*y
+int d = std::is_constant_evaluated(); // initializes d to 1
+int e = d + std::is_constant_evaluated(); // initializes e to 1 + 0
+
+constexpr int
+foo(int x)
+{
+ const int n = std::is_constant_evaluated() ? 13 : 17; // n == 13
+ int m = std::is_constant_evaluated() ? 13 : 17; // m might be 13 or 17 (see below)
+ char arr[n] = {}; // char[13]
+ return m + sizeof (arr) + x;
+}
+
+constexpr int
+bar()
+{
+ const int n = std::is_constant_evaluated() ? 13 : 17;
+ X<n> x1;
+ X<std::is_constant_evaluated() ? 13 : 17> x2;
+ static_assert(std::is_same<decltype(x1), decltype(x2)>::value,
+ "x1/x2's type");
+ return x1.v + x2.v;
+}
+
+int p = foo(0); // m == 13; initialized to 26
+int q = p + foo(0); // m == 17 for this call; initialized to 56
+static_assert(bar() == 26, "bar");
+
+struct S { int a, b; };
+
+S s = { std::is_constant_evaluated() ? 2 : 3, y };
+S t = { std::is_constant_evaluated() ? 2 : 3, 4 };
+
+static_assert(std::is_same<decltype(x), X<true> >::value, "x's type");
+
+void
+test01()
+{
+ VERIFY( a == 1 && b == 2 && c == 8 && d == 1 && e == 1 && p == 26 );
+ VERIFY( q == 56 && s.a == 3 && s.b == 4 && t.a == 2 && t.b == 4 );
+ VERIFY( foo (y) == 34 );
+ if constexpr (foo (0) != 26)
+ VERIFY( 0 );
+ constexpr int w = foo (0);
+ VERIFY( w == 26 );
+}
+
+int main()
+{
+ test01();
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_constant_evaluated/noexcept.cc b/libstdc++-v3/testsuite/20_util/is_constant_evaluated/noexcept.cc
new file mode 100644
index 00000000000..273cec91707
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_constant_evaluated/noexcept.cc
@@ -0,0 +1,23 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <type_traits>
+
+static_assert(noexcept(std::is_constant_evaluated()));