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]

Re: [cxx0x] stl_pair update patch


... ok, the below is what I'm going to commit. I think it's sufficiently small to go in now, waile waiting for the complete paperwork (by the way, how is it going?). Anyway, there are quite a few substantive differences vs the original version, note in particular the relatively tricky changes in the implementation of member swap to conform to the Swappable requirements (because of that I'm adding immediately a testcase using uneq_allocator which is not Copy Assignable (neither Move Assignable)).

Tested x86_64-linux.

Paolo.

/////////////////
2007-10-09  Paolo Carlini  <pcarlini@suse.de>
	    Chris Fairles  <chris.fairles@gmail.com>

	* include/bits/stl_algobase.h (swap): Move...
	* include/bits/stl_move.h: ... here.
	* include/bits/stl_pair.h (pair<>::pair(_U1&&, _U2&&),
	pair<>::pair(pair<>&&), pair<>::operator=(pair<>&&),
	pair<>::swap(pair&&), swap(&, &), swap(&&, &), swap(&, &&),
	make_pair(_T1&&, _T2&&)): Add.
	* testsuite/20_util/pair/swap.cc: Add.
Index: include/bits/stl_algobase.h
===================================================================
--- include/bits/stl_algobase.h	(revision 129164)
+++ include/bits/stl_algobase.h	(working copy)
@@ -73,31 +73,10 @@
 #include <bits/stl_iterator.h>
 #include <bits/concept_check.h>
 #include <debug/debug.h>
-#include <bits/stl_move.h> // For _GLIBCXX_MOVE
+#include <bits/stl_move.h> // For std::swap and _GLIBCXX_MOVE
 
 _GLIBCXX_BEGIN_NAMESPACE(std)
 
-  /**
-   *  @brief Swaps two values.
-   *  @param  a  A thing of arbitrary type.
-   *  @param  b  Another thing of arbitrary type.
-   *  @return   Nothing.
-   *
-   *  This is the simple classic generic implementation.  It will work on
-   *  any type which has a copy constructor and an assignment operator.
-  */
-  template<typename _Tp>
-    inline void
-    swap(_Tp& __a, _Tp& __b)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
-
-      _Tp __tmp = _GLIBCXX_MOVE(__a);
-      __a = _GLIBCXX_MOVE(__b);
-      __b = _GLIBCXX_MOVE(__tmp);
-    }
-
   // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
   // nutshell, we are partially implementing the resolution of DR 187,
   // when it's safe, i.e., the value_types are equal.
Index: include/bits/stl_pair.h
===================================================================
--- include/bits/stl_pair.h	(revision 129164)
+++ include/bits/stl_pair.h	(working copy)
@@ -62,7 +62,8 @@
 #ifndef _STL_PAIR_H
 #define _STL_PAIR_H 1
 
-#include <bits/stl_move.h>
+#include <bits/stl_move.h> // for std::move / std::forward, std::decay, and
+                           // std::swap
 
 _GLIBCXX_BEGIN_NAMESPACE(std)
 
@@ -87,15 +88,28 @@
       pair(const _T1& __a, const _T2& __b)
       : first(__a), second(__b) { }
 
-      /** There is also a templated copy ctor for the @c pair class itself.  */
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
       template<class _U1, class _U2>
-        pair(const pair<_U1, _U2>& __p)
-	: first(__p.first), second(__p.second) { }
+        pair(_U1&& __x, _U2&& __y)
+	: first(std::forward<_U1>(__x)),
+	  second(std::forward<_U2>(__y)) { }
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
       pair(pair&& __p)
       : first(std::move(__p.first)),
 	second(std::move(__p.second)) { }
+#endif
+
+      /** There is also a templated copy ctor for the @c pair class itself.  */
+      template<class _U1, class _U2>
+        pair(const pair<_U1, _U2>& __p)
+	: first(__p.first),
+	  second(__p.second) { }
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+      template<class _U1, class _U2>
+        pair(pair<_U1, _U2>&& __p)
+	: first(std::move(__p.first)),
+	  second(std::move(__p.second)) { }
 
       pair&
       operator=(pair&& __p)
@@ -104,6 +118,23 @@
 	second = std::move(__p.second);
 	return *this;
       }
+
+      template<class _U1, class _U2>
+        pair&
+        operator=(pair<_U1, _U2>&& __p)
+	{
+	  first = std::move(__p.first);
+	  second = std::move(__p.second);
+	  return *this;
+	}
+
+      void
+      swap(pair&& __p)
+      {
+	using std::swap;
+	swap(first, __p.first);
+	swap(second, __p.second);	
+      }
 #endif
     };
 
@@ -144,6 +175,24 @@
     operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     { return !(__x < __y); }
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+  /// See std::pair::swap().
+  template<class _T1, class _T2>
+    inline void
+    swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
+    { __x.swap(__y); }
+
+  template<class _T1, class _T2>
+    inline void
+    swap(pair<_T1, _T2>&& __x, pair<_T1, _T2>& __y)
+    { __x.swap(__y); }
+
+  template<class _T1, class _T2>
+    inline void
+    swap(pair<_T1, _T2>& __x, pair<_T1, _T2>&& __y)
+    { __x.swap(__y); }
+#endif
+
   /**
    *  @brief A convenience wrapper for creating a pair from two objects.
    *  @param  x  The first object.
@@ -156,10 +205,22 @@
    */
   // _GLIBCXX_RESOLVE_LIB_DEFECTS
   // 181.  make_pair() unintended behavior
+#ifndef __GXX_EXPERIMENTAL_CXX0X__
   template<class _T1, class _T2>
     inline pair<_T1, _T2>
     make_pair(_T1 __x, _T2 __y)
     { return pair<_T1, _T2>(__x, __y); }
+#else
+  template<class _T1, class _T2>
+    inline pair<typename std::decay<_T1>::type,
+		typename std::decay<_T2>::type>
+    make_pair(_T1&& __x, _T2&& __y)
+    {
+      return pair<typename std::decay<_T1>::type,
+	          typename std::decay<_T2>::type>(std::forward<_T1>(__x),
+						  std::forward<_T2>(__y));
+    }
+#endif
 
 _GLIBCXX_END_NAMESPACE
 
Index: include/bits/stl_move.h
===================================================================
--- include/bits/stl_move.h	(revision 129164)
+++ include/bits/stl_move.h	(working copy)
@@ -1,4 +1,4 @@
-// Move, forward and identity implementation for C++0x -*- C++ -*-
+// Move, forward and identity for C++0x + swap -*- C++ -*-
 
 // Copyright (C) 2007 Free Software Foundation, Inc.
 //
@@ -35,6 +35,9 @@
 #ifndef _STL_MOVE_H
 #define _STL_MOVE_H 1
 
+#include <bits/c++config.h>
+#include <bits/concept_check.h>
+
 #ifdef __GXX_EXPERIMENTAL_CXX0X__
 #include <type_traits>
 
@@ -64,4 +67,26 @@
 #define _GLIBCXX_MOVE(_Tp) _Tp
 #endif
 
+_GLIBCXX_BEGIN_NAMESPACE(std)
+
+  /**
+   *  @brief Swaps two values.
+   *  @param  a  A thing of arbitrary type.
+   *  @param  b  Another thing of arbitrary type.
+   *  @return   Nothing.
+  */
+  template<typename _Tp>
+    inline void
+    swap(_Tp& __a, _Tp& __b)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
+
+      _Tp __tmp = _GLIBCXX_MOVE(__a);
+      __a = _GLIBCXX_MOVE(__b);
+      __b = _GLIBCXX_MOVE(__tmp);
+    }
+
+_GLIBCXX_END_NAMESPACE
+
 #endif /* _STL_MOVE_H */
Index: testsuite/20_util/pair/swap.cc
===================================================================
--- testsuite/20_util/pair/swap.cc	(revision 0)
+++ testsuite/20_util/pair/swap.cc	(revision 0)
@@ -0,0 +1,59 @@
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2007 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// As a special exception, you may use this file as part of a free software
+// library without restriction.  Specifically, if other files instantiate
+// templates or use macros or inline functions from this file, or you compile
+// this file and link it with other files to produce an executable, this
+// file does not by itself cause the resulting executable to be covered by
+// the GNU General Public License.  This exception does not however
+// invalidate any other reasons why the executable file might be covered by
+// the GNU General Public License.
+
+#include <utility>
+#include <testsuite_allocator.h>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  typedef __gnu_test::uneq_allocator<double> ua_type;
+  ua_type one(1), two(2);
+
+  std::pair<ua_type, int> p1(one, 1), p2(two, 2);
+
+  p1.swap(p2);
+  VERIFY( p1.first.get_personality() == 2 );
+  VERIFY( p1.second == 2 );
+  VERIFY( p2.first.get_personality() == 1 );
+  VERIFY( p2.second == 1 );
+
+  swap(p1, p2);
+  VERIFY( p2.first.get_personality() == 2 );
+  VERIFY( p2.second == 2 );
+  VERIFY( p1.first.get_personality() == 1 );
+  VERIFY( p1.second == 1 );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}

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