OSDN Git Service

2007-10-10 Paolo Carlini <pcarlini@suse.de>
authorpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 10 Oct 2007 09:29:11 +0000 (09:29 +0000)
committerpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 10 Oct 2007 09:29:11 +0000 (09:29 +0000)
    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.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@129198 138bc75d-0d04-0410-961f-82ee72b054a4

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/stl_algobase.h
libstdc++-v3/include/bits/stl_move.h
libstdc++-v3/include/bits/stl_pair.h
libstdc++-v3/testsuite/20_util/pair/swap.cc [new file with mode: 0644]

index 7b96f32..44afe2e 100644 (file)
@@ -1,3 +1,14 @@
+2007-10-10  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.
+
 2007-10-09  Wolfgang Bangerth  <bangerth@dealii.org>
 
        PR libstdc++/33485 continued.
index 64b13a5..c641df5 100644 (file)
 #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 3c799e4..f8e1062 100644 (file)
@@ -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 @@ _GLIBCXX_END_NAMESPACE
 #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 68927d2..7163b62 100644 (file)
@@ -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 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       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 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        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 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     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_BEGIN_NAMESPACE(std)
    */
   // _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
 
diff --git a/libstdc++-v3/testsuite/20_util/pair/swap.cc b/libstdc++-v3/testsuite/20_util/pair/swap.cc
new file mode 100644 (file)
index 0000000..e045097
--- /dev/null
@@ -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;
+}