[gcc r10-9679] libstdc++: Fix invalid constexpr function in C++11 mode [PR 99985]

Jonathan Wakely redi@gcc.gnu.org
Fri Apr 9 15:15:03 GMT 2021


https://gcc.gnu.org/g:637418ec2c6f559d4fac074db3bafc34a728484b

commit r10-9679-g637418ec2c6f559d4fac074db3bafc34a728484b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Apr 9 12:05:39 2021 +0100

    libstdc++: Fix invalid constexpr function in C++11 mode [PR 99985]
    
    I keep forgetting that a constexpr function in C++11 has to be a single
    return statement.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/99985
            * include/bits/hashtable.h (_Hashtable::_S_nothrow_move()): Fix
            to be a valid constexpr function in C++11.
            * testsuite/23_containers/unordered_set/cons/99985.cc: New test.
    
    (cherry picked from commit 40ccb47b505b528244ee305923681c0ae3b6f4d5)

Diff:
---
 libstdc++-v3/include/bits/hashtable.h              | 10 ++++-
 .../23_containers/unordered_set/cons/99985.cc      | 47 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h
index 8705d1f33b8..e9a6cfe8b83 100644
--- a/libstdc++-v3/include/bits/hashtable.h
+++ b/libstdc++-v3/include/bits/hashtable.h
@@ -450,10 +450,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	static constexpr bool
 	_S_nothrow_move()
 	{
-	  if _GLIBCXX17_CONSTEXPR (_No_realloc)
-	    if _GLIBCXX17_CONSTEXPR (is_nothrow_copy_constructible<_H1>())
+#if __cplusplus <= 201402L
+	  return __and_<__bool_constant<_No_realloc>,
+			is_nothrow_copy_constructible<_H1>,
+			is_nothrow_copy_constructible<_Equal>>::value;
+#else
+	  if constexpr (_No_realloc)
+	    if constexpr (is_nothrow_copy_constructible<_H1>())
 	      return is_nothrow_copy_constructible<_Equal>();
 	  return false;
+#endif
 	}
 
       _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/cons/99985.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/99985.cc
new file mode 100644
index 00000000000..b209f7627f5
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/99985.cc
@@ -0,0 +1,47 @@
+// Copyright (C) 2021 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++11" }
+// { dg-do compile { target c++11 } }
+
+#include <unordered_set>
+#include <testsuite_allocator.h>
+
+template<typename Alloc, typename T = typename Alloc::value_type>
+  using Set = std::unordered_set<T, std::hash<T>, std::equal_to<T>, Alloc>;
+
+// PR libstdc++/99985 - invalid constexpr function in C++11 mode
+
+void
+test01()
+{
+  using A = std::allocator<int>;
+  A a;
+  Set<A> s;
+  static_assert( noexcept( Set<A>(std::move(s)) ), "non-throwing" );
+  static_assert( noexcept( Set<A>(std::move(s), a) ), "non-throwing" );
+}
+
+void
+test02()
+{
+  using A = __gnu_test::uneq_allocator<long>;
+  A a;
+  Set<A> s;
+  static_assert( noexcept( Set<A>(std::move(s)) ), "non-throwing" );
+  static_assert( ! noexcept( Set<A>(std::move(s), a) ), "throwing" );
+}


More information about the Libstdc++-cvs mailing list