This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] constexpr pair additions


Add constexpr markup to move/forward, and enable more std::pair
constructors. 

tested x86/linux

-benjamin
2011-07-22  Benjamin Kosnik  <bkoz@redhat.com>
	    Daniel Krugler  <daniel.kruegler@googlemail.com>

	* include/bits/move.h (move, forward): Mark constexpr.
	* include/bits/stl_pair.h (pair): Mark move ctors constexpr.
	* testsuite/20_util/pair/make_pair/constexpr.cc: New.
	* testsuite/20_util/pair/cons/constexpr.cc: Add tests.


Index: include/bits/move.h
===================================================================
--- include/bits/move.h	(revision 176670)
+++ include/bits/move.h	(working copy)
@@ -58,12 +58,12 @@
   
   /// forward (as per N3143)
   template<typename _Tp>
-    inline _Tp&&
+    inline constexpr _Tp&&
     forward(typename std::remove_reference<_Tp>::type& __t) noexcept
     { return static_cast<_Tp&&>(__t); }
 
   template<typename _Tp>
-    inline _Tp&&
+    inline constexpr _Tp&&
     forward(typename std::remove_reference<_Tp>::type&& __t) noexcept
     {
       static_assert(!std::is_lvalue_reference<_Tp>::value, "template argument"
@@ -78,7 +78,7 @@
    *  @return Same, moved.
   */
   template<typename _Tp>
-    inline typename std::remove_reference<_Tp>::type&&
+    inline constexpr typename std::remove_reference<_Tp>::type&&
     move(_Tp&& __t) noexcept
     { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
 
Index: include/bits/stl_pair.h
===================================================================
--- include/bits/stl_pair.h	(revision 176670)
+++ include/bits/stl_pair.h	(working copy)
@@ -128,24 +128,24 @@
       // DR 811.
       template<class _U1, class = typename
 	       enable_if<is_convertible<_U1, _T1>::value>::type>
-	pair(_U1&& __x, const _T2& __y)
+	constexpr pair(_U1&& __x, const _T2& __y)
 	: first(std::forward<_U1>(__x)), second(__y) { }
 
       template<class _U2, class = typename
 	       enable_if<is_convertible<_U2, _T2>::value>::type>
-	pair(const _T1& __x, _U2&& __y)
+	constexpr pair(const _T1& __x, _U2&& __y)
 	: first(__x), second(std::forward<_U2>(__y)) { }
 
       template<class _U1, class _U2, class = typename
 	       enable_if<__and_<is_convertible<_U1, _T1>,
 				is_convertible<_U2, _T2>>::value>::type>
-	pair(_U1&& __x, _U2&& __y)
+	constexpr pair(_U1&& __x, _U2&& __y)
 	: first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
 
       template<class _U1, class _U2, class = typename
 	       enable_if<__and_<is_convertible<_U1, _T1>,
 				is_convertible<_U2, _T2>>::value>::type>
-	pair(pair<_U1, _U2>&& __p)
+	constexpr pair(pair<_U1, _U2>&& __p)
 	: first(std::forward<_U1>(__p.first)),
 	  second(std::forward<_U2>(__p.second)) { }
 
@@ -275,8 +275,8 @@
 #ifdef __GXX_EXPERIMENTAL_CXX0X__
   // NB: DR 706.
   template<class _T1, class _T2>
-    inline pair<typename __decay_and_strip<_T1>::__type,
-		typename __decay_and_strip<_T2>::__type>
+    inline constexpr pair<typename __decay_and_strip<_T1>::__type,
+			  typename __decay_and_strip<_T2>::__type>
     make_pair(_T1&& __x, _T2&& __y)
     {
       typedef typename __decay_and_strip<_T1>::__type __ds_type1;
Index: testsuite/20_util/pair/make_pair/constexpr.cc
===================================================================
--- testsuite/20_util/pair/make_pair/constexpr.cc	(revision 0)
+++ testsuite/20_util/pair/make_pair/constexpr.cc	(revision 0)
@@ -0,0 +1,42 @@
+// { dg-do compile }
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2011 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/>.
+
+
+// NOTE: This makes use of the fact that we know how moveable
+// is implemented on pair, and also vector. If the implementation 
+// changes this test may begin to fail.
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+void
+test1()
+{
+  bool test __attribute__((unused)) = true;
+  typedef std::pair<int, float> pair_type;
+  constexpr pair_type p1 = std::make_pair(22, 22.222);
+}
+
+int 
+main() 
+{
+  test1();
+  return 0;
+}
Index: testsuite/20_util/pair/cons/constexpr.cc
===================================================================
--- testsuite/20_util/pair/cons/constexpr.cc	(revision 176670)
+++ testsuite/20_util/pair/cons/constexpr.cc	(working copy)
@@ -1,7 +1,7 @@
 // { dg-do compile }
 // { dg-options "-std=gnu++0x" }
 
-// Copyright (C) 2010 Free Software Foundation, Inc.
+// Copyright (C) 2010, 2011 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
@@ -23,17 +23,33 @@
 
 int main()
 {
+  typedef std::pair<int, int> pair_type;
+
   __gnu_test::constexpr_default_constructible test1;
-  test1.operator()<std::pair<int, int>>();
+  test1.operator()<pair_type>();
 
   __gnu_test::constexpr_single_value_constructible test2;
-  test2.operator()<std::pair<int, int>, std::pair<int, int>>();
-  test2.operator()<std::pair<int, int>, std::pair<short, short>>();
+  test2.operator()<pair_type, pair_type>();
+  test2.operator()<pair_type, std::pair<short, short>>();
 
   // test 3
   const int i1(129);
   const int i2(6);
-  constexpr std::pair<int, int> p3(i1, i2);
+  constexpr pair_type p0(i1, i2);
 
+  // test 4
+  constexpr int i(999);
+  constexpr pair_type p1 { 44, 90 };
+  constexpr pair_type p2 { std::move(p1.first),  i };
+  constexpr pair_type p3 { i, std::move(p1.second) };
+
+  constexpr pair_type p5 { 444, 904 };
+  constexpr pair_type p6 { std::move(p5.first), std::move(p5.second) };
+
+  constexpr std::pair<char, char> p8 { 'a', 'z' };
+  constexpr pair_type p9(std::move(p8));
+
+  constexpr pair_type p10(std::move(p0));
+
   return 0;
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]