Patchwork [v3] more constexpr hacking

login
register
mail settings
Submitter Benjamin Kosnik
Date Oct. 18, 2012, 8:39 a.m.
Message ID <20121018013929.0a48fb52@coso>
Download mbox | patch
Permalink /patch/192240/
State New
Headers show

Comments

Benjamin Kosnik - Oct. 18, 2012, 8:39 a.m.
.... as per LWG comments on N330[2-5].

Couple of notes: new fail has been temporarily worked around pending
the resolution of 

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54968

For <tuple>, the big fun remaining is piecewise_construct_t, which
should allow tie and forward_as_tuple to be constexpr. I'm too tired to
get into this with -fconstexpr-depth=2 tonight.

Of course, if Daniel or Jonathan want to hack on this while I'm asleep,
well then....

tested x86/linux

-benjamin

Patch

2012-10-09  Benjamin Kosnik  <bkoz@redhat.com>

	* include/bits/move.h (move_if_noexcept): Mark constexpr.
	* include/std/array (front, back): Same.
	* include/std/chrono: Add comment.
	* include/std/tuple (__tuple_compare): Mark __eq, __less constexpr.
	(operator ==, <, >, !=, <=, >=): Same.
	* testsuite/20_util/forward/c_neg.cc: Adjust line numbers.
	* testsuite/20_util/forward/f_neg.cc: Same.
	* testsuite/20_util/move_if_noexcept/constexpr.cc: New.
	* testsuite/20_util/tuple/comparison_operators/constexpr.cc: New.
	* testsuite/20_util/tuple/creation_functions/constexpr.cc: Add.
	* testsuite/23_containers/array/element_access/
	constexpr_element_access.cc: Same.
	* testsuite/23_containers/array/tuple_interface/get_neg.cc: Adjust
	line numbers.
	* testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc:
	Same.

	* testsuite/20_util/tuple/comparison_operators/35480_neg.cc:
          Temporarily add dg-excess-errors.

diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h
index 353c466..236f0de 100644
--- a/libstdc++-v3/include/bits/move.h
+++ b/libstdc++-v3/include/bits/move.h
@@ -1,6 +1,6 @@ 
 // Move, forward and identity for C++0x + swap -*- C++ -*-
 
-// Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+// Copyright (C) 2007-2012 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
@@ -65,7 +65,6 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  @{
    */
 
-  // forward (as per N3143)
   /**
    *  @brief  Forward an lvalue.
    *  @return The parameter cast to the specified type.
@@ -117,7 +116,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  type is copyable, in which case an lvalue-reference is returned instead.
    */
   template<typename _Tp>
-    inline typename
+    inline constexpr typename
     conditional<__move_if_noexcept_cond<_Tp>::value, const _Tp&, _Tp&&>::type
     move_if_noexcept(_Tp& __x) noexcept
     { return std::move(__x); }
diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array
index c7c0a5a..15dd6c1 100644
--- a/libstdc++-v3/include/std/array
+++ b/libstdc++-v3/include/std/array
@@ -198,17 +198,20 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       front()
       { return *begin(); }
 
-      const_reference 
+      constexpr const_reference 
       front() const
-      { return *begin(); }
+      { return _AT_Type::_S_ref(_M_elems, 0); }
 
       reference 
       back()
       { return _Nm ? *(end() - 1) : *end(); }
 
-      const_reference 
+      constexpr const_reference 
       back() const
-      { return _Nm ? *(end() - 1) : *end(); }
+      { 
+	return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1) 
+ 	           : _AT_Type::_S_ref(_M_elems, _Nm); 
+      }
 
       pointer
       data() noexcept
diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index 209f395..d920a7d 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -250,7 +250,10 @@  _GLIBCXX_END_NAMESPACE_VERSION
 	// 20.11.5.1 construction / copy / destroy
 	constexpr duration() = default;
 
-	constexpr duration(const duration&) = default;
+	// NB: Make constexpr implicit. This cannot be explicitly
+	// constexpr, as any UDT that is not a literal type with a
+	// constexpr copy constructor will be ill-formed.
+	duration(const duration&) = default;
 
 	template<typename _Rep2, typename = typename
 	       enable_if<is_convertible<_Rep2, rep>::value
diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple
index fb9e09f..b4985d2 100644
--- a/libstdc++-v3/include/std/tuple
+++ b/libstdc++-v3/include/std/tuple
@@ -775,14 +775,14 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
     struct __tuple_compare<0, __i, __j, _Tp, _Up>
     {
-      static bool 
+      static constexpr bool 
       __eq(const _Tp& __t, const _Up& __u)
       {
 	return (get<__i>(__t) == get<__i>(__u) &&
 		__tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
       }
      
-      static bool 
+      static constexpr bool 
       __less(const _Tp& __t, const _Up& __u)
       {
 	return ((get<__i>(__t) < get<__i>(__u))
@@ -794,55 +794,55 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<std::size_t __i, typename _Tp, typename _Up>
     struct __tuple_compare<0, __i, __i, _Tp, _Up>
     {
-      static bool 
+      static constexpr bool 
       __eq(const _Tp&, const _Up&) { return true; }
      
-      static bool 
+      static constexpr bool 
       __less(const _Tp&, const _Up&) { return false; }
     };
 
   template<typename... _TElements, typename... _UElements>
-    bool
+    constexpr bool
     operator==(const tuple<_TElements...>& __t,
 	       const tuple<_UElements...>& __u)
     {
       typedef tuple<_TElements...> _Tp;
       typedef tuple<_UElements...> _Up;
-      return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
+      return bool(__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
 	      0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
     }
 
   template<typename... _TElements, typename... _UElements>
-    bool
+    constexpr bool
     operator<(const tuple<_TElements...>& __t,
 	      const tuple<_UElements...>& __u)
     {
       typedef tuple<_TElements...> _Tp;
       typedef tuple<_UElements...> _Up;
-      return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
+      return bool(__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
 	      0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
     }
 
   template<typename... _TElements, typename... _UElements>
-    inline bool
+    inline constexpr bool
     operator!=(const tuple<_TElements...>& __t,
 	       const tuple<_UElements...>& __u)
     { return !(__t == __u); }
 
   template<typename... _TElements, typename... _UElements>
-    inline bool
+    inline constexpr bool
     operator>(const tuple<_TElements...>& __t,
 	      const tuple<_UElements...>& __u)
     { return __u < __t; }
 
   template<typename... _TElements, typename... _UElements>
-    inline bool
+    inline constexpr bool
     operator<=(const tuple<_TElements...>& __t,
 	       const tuple<_UElements...>& __u)
     { return !(__u < __t); }
 
   template<typename... _TElements, typename... _UElements>
-    inline bool
+    inline constexpr bool
     operator>=(const tuple<_TElements...>& __t,
 	       const tuple<_UElements...>& __u)
     { return !(__t < __u); }
@@ -858,7 +858,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 
   template<typename... _Elements>
-    constexpr tuple<_Elements&&...>
+    tuple<_Elements&&...>
     forward_as_tuple(_Elements&&... __args) noexcept
     { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
 
diff --git a/libstdc++-v3/testsuite/20_util/forward/c_neg.cc b/libstdc++-v3/testsuite/20_util/forward/c_neg.cc
index 0112824..1e573ec 100644
--- a/libstdc++-v3/testsuite/20_util/forward/c_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/forward/c_neg.cc
@@ -1,7 +1,7 @@ 
 // { dg-do compile }
 // { dg-options "-std=gnu++0x" }
 
-// Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+// Copyright (C) 2010-2012 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
@@ -18,7 +18,7 @@ 
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-error "static assertion failed" "" { target *-*-* } 90 }
+// { dg-error "static assertion failed" "" { target *-*-* } 89 }
 
 #include <list>
 
diff --git a/libstdc++-v3/testsuite/20_util/forward/f_neg.cc b/libstdc++-v3/testsuite/20_util/forward/f_neg.cc
index 9e5b78a..d4a9c7a 100644
--- a/libstdc++-v3/testsuite/20_util/forward/f_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/forward/f_neg.cc
@@ -1,7 +1,7 @@ 
 // { dg-do compile }
 // { dg-options "-std=gnu++0x" }
 
-// Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+// Copyright (C) 2010-2012 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
@@ -18,7 +18,7 @@ 
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-error "static assertion failed" "" { target *-*-* } 90 }
+// { dg-error "static assertion failed" "" { target *-*-* } 89 }
 
 #include <utility>
 
diff --git a/libstdc++-v3/testsuite/20_util/move_if_noexcept/constexpr.cc b/libstdc++-v3/testsuite/20_util/move_if_noexcept/constexpr.cc
new file mode 100644
index 0000000..4811b17
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/move_if_noexcept/constexpr.cc
@@ -0,0 +1,42 @@ 
+// { dg-do compile }
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2012 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 <utility>
+#include <testsuite_hooks.h>
+
+struct simple
+{
+  int i;
+};
+
+void
+test01()
+{
+  bool test __attribute__((unused)) = true;
+
+  constexpr simple s { 5 };
+  constexpr auto s2  __attribute__((unused)) = std::move_if_noexcept(s);
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/35480_neg.cc b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/35480_neg.cc
index eb22938..eb4c213 100644
--- a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/35480_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/35480_neg.cc
@@ -1,7 +1,7 @@ 
 // { dg-options "-std=gnu++0x" }
 // { dg-do compile }
 
-// Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+// Copyright (C) 2008, 2009, 2012 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
@@ -31,3 +31,4 @@  void test01()
   if ( t1 == t2 ) {}  // { dg-error "here" }
 }
 // { dg-prune-output "incomplete type" }
+// { dg-excess-errors "body of constexpr function" }
diff --git a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/constexpr.cc b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/constexpr.cc
new file mode 100644
index 0000000..0efb4c3
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/constexpr.cc
@@ -0,0 +1,29 @@ 
+// { dg-do compile }
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2012 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 <tuple>
+#include <testsuite_common_types.h>
+
+int main()
+{
+  __gnu_test::constexpr_comparison_operators test;
+  test.operator()<std::tuple<int, int>>();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/20_util/tuple/creation_functions/constexpr.cc b/libstdc++-v3/testsuite/20_util/tuple/creation_functions/constexpr.cc
index bf2a857..6c26060 100644
--- a/libstdc++-v3/testsuite/20_util/tuple/creation_functions/constexpr.cc
+++ b/libstdc++-v3/testsuite/20_util/tuple/creation_functions/constexpr.cc
@@ -1,7 +1,7 @@ 
 // { dg-do compile }
 // { dg-options "-std=gnu++0x" }
 
-// Copyright (C) 2011 Free Software Foundation, Inc.
+// Copyright (C) 2011, 2012 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
@@ -45,6 +45,50 @@  test_make_tuple()
   }
 }
 
+#if 0
+// forward_as_tuple
+void
+test_forward_as_tuple()
+{
+  {
+    typedef std::tuple<int, float> tuple_type;
+    constexpr tuple_type p1 __attribute__((unused))
+      = std::forward_as_tuple(22, 22.222);
+  }
+
+  {
+    typedef std::tuple<int, float, int> tuple_type;
+    constexpr tuple_type p1 __attribute__((unused))
+      = std::forward_as_tuple(22, 22.222, 77799);
+  }
+}
+#endif
+
+#if 0
+// tie
+void
+test_tie()
+{
+  {
+    int i(22);
+    float f(22.222);
+    typedef std::tuple<int, float> tuple_type;
+    constexpr tuple_type p1 __attribute__((unused))
+      = std::tie(i, f);
+  }
+
+  {
+    int i(22);
+    float f(22.222);
+    int ii(77799);
+
+    typedef std::tuple<int, float, int> tuple_type;
+    constexpr tuple_type p1 __attribute__((unused))
+      = std::tie(i, f, ii);
+  }
+}
+#endif
+
 // get
 void
 test_get()
diff --git a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_element_access.cc b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_element_access.cc
index c2f301a..ec46ac0 100644
--- a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_element_access.cc
+++ b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_element_access.cc
@@ -1,7 +1,7 @@ 
 // { dg-do compile }
 // { dg-options "-std=gnu++0x" }
 
-// Copyright (C) 2011 Free Software Foundation, Inc.
+// Copyright (C) 2011-2012 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
@@ -27,5 +27,7 @@  int main()
   constexpr array_type a = { { 0, 55, 66, 99, 4115, 2 } };
   constexpr auto v1 __attribute__((unused)) = a[1];
   constexpr auto v2 __attribute__((unused)) = a.at(2);
+  constexpr auto v3 __attribute__((unused)) = a.front();
+  constexpr auto v4 __attribute__((unused)) = a.back();
   return 0;
 }
diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc
index e74af1b..7c7a365 100644
--- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc
+++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc
@@ -27,6 +27,6 @@  int n1 = std::get<1>(a);
 int n2 = std::get<1>(std::move(a));
 int n3 = std::get<1>(ca);
 
-// { dg-error "static assertion failed" "" { target *-*-* } 288 }
-// { dg-error "static assertion failed" "" { target *-*-* } 296 }
-// { dg-error "static assertion failed" "" { target *-*-* } 304 }
+// { dg-error "static assertion failed" "" { target *-*-* } 291 }
+// { dg-error "static assertion failed" "" { target *-*-* } 299 }
+// { dg-error "static assertion failed" "" { target *-*-* } 307 }
diff --git a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc
index b9ce910..3c642c8 100644
--- a/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc
+++ b/libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc
@@ -22,4 +22,4 @@ 
 
 typedef std::tuple_element<1, std::array<int, 1>>::type type;
 
-// { dg-error "static assertion failed" "" { target *-*-* } 280 }
+// { dg-error "static assertion failed" "" { target *-*-* } 283 }