tuple (tuple<>::swap): Implement swap for tuple as per DR 522 [Ready].

2008-09-30  Chris Fairles  <cfairles@gcc.gnu.org>

        * include/std/tuple (tuple<>::swap): Implement swap for tuple as per
        DR 522 [Ready].
        * testsuite/20_util/tuple/swap.cc: New.

From-SVN: r140822
This commit is contained in:
Chris Fairles 2008-10-01 22:04:02 +00:00
parent 266b48906a
commit 3e93b27552
3 changed files with 176 additions and 2 deletions

View File

@ -1,3 +1,9 @@
2008-09-30 Chris Fairles <cfairles@gcc.gnu.org>
* include/std/tuple (tuple<>::swap): Implement swap for tuple as per
DR 522 [Ready].
* testsuite/20_util/tuple/swap.cc: New.
2008-09-30 Paolo Carlini <paolo.carlini@oracle.com>
PR libstdc++/30085 (again)

View File

@ -81,6 +81,8 @@ namespace std
_Head& _M_head() { return *this; }
const _Head& _M_head() const { return *this; }
void __swap_impl(_Head&&) { /* no-op */ }
};
template<std::size_t _Idx, typename _Head>
@ -99,6 +101,13 @@ namespace std
_Head& _M_head() { return _M_head_impl; }
const _Head& _M_head() const { return _M_head_impl; }
void
__swap_impl(_Head&& __h)
{
using std::swap;
swap(__h, _M_head_impl);
}
_Head _M_head_impl;
};
@ -118,7 +127,10 @@ namespace std
* inheritance recursion.
*/
template<std::size_t _Idx>
struct _Tuple_impl<_Idx> { };
struct _Tuple_impl<_Idx>
{
void __swap_impl(_Tuple_impl&&) { /* no-op */ }
};
/**
* Recursive tuple implementation. Here we store the @c Head element
@ -203,6 +215,13 @@ namespace std
_M_tail() = std::move(__in._M_tail());
return *this;
}
void
__swap_impl(_Tuple_impl&& __in)
{
_Base::__swap_impl(__in._M_head());
_Inherited::__swap_impl(__in._M_tail());
}
};
/// tuple
@ -274,11 +293,19 @@ namespace std
static_cast<_Inherited&>(*this) = std::move(__in);
return *this;
}
void
swap(tuple&& __in)
{ _Inherited::__swap_impl(__in); }
};
template<>
class tuple<> { };
class tuple<>
{
public:
void swap(tuple&&) { /* no-op */ }
};
/// tuple (2-element), with construction and assignment from a pair.
template<typename _T1, typename _T2>
@ -368,6 +395,14 @@ namespace std
this->_M_tail()._M_head() = std::move(__in.second);
return *this;
}
void
swap(tuple&& __in)
{
using std::swap;
swap(this->_M_head(), __in._M_head());
swap(this->_M_tail()._M_head(), __in._M_tail()._M_head());
}
};
@ -628,6 +663,21 @@ namespace std
tie(_Elements&... __args)
{ return tuple<_Elements&...>(__args...); }
template<typename... _Elements>
inline void
swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
{ __x.swap(__y); }
template<typename... _Elements>
inline void
swap(tuple<_Elements...>&& __x, tuple<_Elements...>& __y)
{ __x.swap(__y); }
template<typename... _Elements>
inline void
swap(tuple<_Elements...>& __x, tuple<_Elements...>&& __y)
{ __x.swap(__y); }
// A class (and instance) which can be used in 'tie' when an element
// of a tuple is not required
struct _Swallow_assign

View File

@ -0,0 +1,118 @@
// { 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.
// NOTE: This makes use of the fact that we know how moveable
// is implemented on tuple. If the implementation changed
// this test may begin to fail.
#include <tuple>
#include <utility>
#include <testsuite_hooks.h>
struct MoveOnly
{
explicit MoveOnly (int j) : i(j) { }
MoveOnly (MoveOnly&& m) : i(m.i) { }
MoveOnly& operator=(MoveOnly&& m)
{ i = m.i; return *this; }
MoveOnly(MoveOnly const&) = delete;
MoveOnly& operator=(MoveOnly const&) = delete;
bool operator==(MoveOnly const& m)
{ return i == m.i; }
void swap(MoveOnly&& m)
{ std::swap(m.i, i); }
int i;
};
void swap(MoveOnly& m1, MoveOnly& m2)
{ m1.swap(m2); }
MoveOnly
make_move_only (int i)
{ return MoveOnly(i); }
void test01()
{
std::tuple<> t1, t2;
std::swap(t1, t2);
VERIFY( t1 == t2 );
}
void test02()
{
bool test __attribute__((unused)) = true;
std::tuple<int> t1(1), t2(2);
std::swap(t1, t2);
VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 );
}
void test03()
{
bool test __attribute__((unused)) = true;
std::tuple<int, float> t1(1, 1.0f), t2(2, 2.0f);
std::swap(t1, t2);
VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 );
VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f );
}
void test04()
{
bool test __attribute__((unused)) = true;
std::tuple<int, float, MoveOnly>
t1(1, 1.0f, make_move_only(1)),
t2(2, 2.0f, make_move_only(2));
std::swap(t1, t2);
VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 );
VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f );
VERIFY( std::get<2>(t1) == make_move_only(2)
&& std::get<2>(t2) == make_move_only(1) );
}
int main()
{
test01();
test02();
test03();
test04();
return 0;
}