This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[c++0x] tuple rework
- From: Pedro LamarÃo <pedro dot lamarao at mndfck dot org>
- To: libstdc++ at gcc dot gnu dot org
- Date: Fri, 15 Feb 2008 21:25:18 -0200
- Subject: [c++0x] tuple rework
Attached is a patch to the <tuple> header.
The reason they fail is discussed in another messge I sent today.
I'm not sending this patch to gcc-patches with a Changelog entry because
of it provokes new test case failures and because I feel I'm stepping in
the limits of my C++0x knowledge.
This patch changes basically these things:
*) all __add_c_ref and remove_references uses were removed and replaced
with simple const& and && variations;
*) all tuple constructors taking a tuple argument convert the argument
explicitly to it's corresponding _Tuple_base in order to pick the
correct base constructor;
*) all template constructors taking a tuple&& argument std::forward the
tuple elements instead of std::move'ing them when initializing the
contained element.
I believe the remove_reference and __add_c_ref machinery was put in
place to deal with the case where the tuple element type is a T& .
Properly std::forward'ing elements in initialization solves this difficulty.
This patch makes the attached test case PASS.
Four other tests FAIL, on the other hand.
FAIL: 20_util/tuple/comparison_operators/comparisons.cc (test for excess
errors)
WARNING: 20_util/tuple/comparison_operators/comparisons.cc compilation
failed to produce executable
FAIL: 20_util/tuple/cons/assignment.cc (test for excess errors)
WARNING: 20_util/tuple/cons/assignment.cc compilation failed to produce
executable
FAIL: 20_util/tuple/cons/big_tuples.cc (test for excess errors)
WARNING: 20_util/tuple/cons/big_tuples.cc compilation failed to produce
executable
FAIL: 20_util/tuple/cons/constructor.cc (test for excess errors)
WARNING: 20_util/tuple/cons/constructor.cc compilation failed to produce
executable
All these failures are similar to the problem I describe in another
email: when passing a tuple object to initialize another tuple object
the wrong constructor is being selected.
--
P.
// { 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 <tuple>
#include <utility>
#include <testsuite_hooks.h>
class MoveOnly {
public:
MoveOnly () { }
MoveOnly (MoveOnly&&) { }
MoveOnly& operator= (MoveOnly&&) { return *this; }
private:
MoveOnly (MoveOnly const&); // = delete
MoveOnly& operator= (MoveOnly const&); // = delete
};
MoveOnly
make_move_only () { return MoveOnly(); }
int main()
{
bool test __attribute__((unused)) = true;
std::tuple<MoveOnly> t1(make_move_only());
std::tuple<MoveOnly> t2(std::move(t1));
std::tuple<MoveOnly> t3 = std::move(t2);
t1 = std::move(t3);
return 0;
}
Index: /home/pedro.lamarao/Projetos/gcc/mainline/libstdc++-v3/include/std/tuple
===================================================================
--- /home/pedro.lamarao/Projetos/gcc/mainline/libstdc++-v3/include/std/tuple (revisão 132322)
+++ /home/pedro.lamarao/Projetos/gcc/mainline/libstdc++-v3/include/std/tuple (cópia de trabalho)
@@ -72,11 +72,12 @@
_Head_base()
: _Head() { }
- _Head_base(typename __add_c_ref<_Head>::type __h)
+ _Head_base(const _Head& __h)
: _Head(__h) { }
- _Head_base(typename std::remove_reference<_Head>::type&& __h)
- : _Head(std::forward<_Head>(__h)) { }
+ template <typename _UHead>
+ _Head_base(_UHead&& __h)
+ : _Head(std::forward<_UHead>(__h)) { }
_Head& _M_head() { return *this; }
const _Head& _M_head() const { return *this; }
@@ -88,11 +89,12 @@
_Head_base()
: _M_head_impl() { }
- _Head_base(typename __add_c_ref<_Head>::type __h)
+ _Head_base(const _Head& __h)
: _M_head_impl(__h) { }
- _Head_base(typename std::remove_reference<_Head>::type&& __h)
- : _M_head_impl(std::move(__h)) { }
+ template <typename _UHead>
+ _Head_base(_UHead&& __h)
+ : _M_head_impl(std::forward<_UHead>(__h)) { }
_Head& _M_head() { return _M_head_impl; }
const _Head& _M_head() const { return _M_head_impl; }
@@ -141,23 +143,23 @@
: _Inherited(), _Base() { }
explicit
- _Tuple_impl(typename __add_c_ref<_Head>::type __head,
- typename __add_c_ref<_Tail>::type... __tail)
+ _Tuple_impl(const _Head& __head,
+ const _Tail&... __tail)
: _Inherited(__tail...), _Base(__head) { }
template<typename _UHead, typename... _UTail>
explicit
- _Tuple_impl(typename std::remove_reference<_UHead>::type&& __head,
- typename std::remove_reference<_UTail>::type&&... __tail)
- : _Inherited(std::forward<_Inherited>(__tail)...),
- _Base(std::forward<_Base>(__head)) { }
+ _Tuple_impl(_UHead&& __head,
+ _UTail&&... __tail)
+ : _Inherited(std::forward<_UTail>(__tail)...),
+ _Base(std::forward<_UHead>(__head)) { }
_Tuple_impl(const _Tuple_impl& __in)
: _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
_Tuple_impl(_Tuple_impl&& __in)
- : _Inherited(std::forward<_Inherited>(__in._M_tail())),
- _Base(std::forward<_Base>(__in._M_head())) { }
+ : _Inherited(std::move<_Inherited&&>(__in._M_tail())),
+ _Base(std::forward<_Head>(__in._M_head())) { }
template<typename... _UElements>
_Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
@@ -165,8 +167,8 @@
template<typename... _UElements>
_Tuple_impl(_Tuple_impl<_Idx, _UElements...>&& __in)
- : _Inherited(std::forward<_Inherited>(__in._M_tail())),
- _Base(std::forward<_Base>(__in._M_head())) { }
+ : _Inherited(std::move<typename _Tuple_impl<_Idx, _UElements...>::_Inherited&&>(__in._M_tail())),
+ _Base(std::forward<typename _Tuple_impl<_Idx, _UElements...>::_Base>(__in._M_head())) { }
_Tuple_impl&
operator=(const _Tuple_impl& __in)
@@ -219,21 +221,21 @@
template<typename... _UElements>
explicit
tuple(_UElements&&... __elements)
- : _Inherited(std::forward<_UElements>(__elements)...) { }
+ : _Inherited(std::forward<_UElements>(__elements)...) { }
tuple(const tuple& __in)
- : _Inherited(__in) { }
+ : _Inherited(static_cast<const _Inherited&>(__in)) { }
tuple(tuple&& __in)
- : _Inherited(std::move(__in)) { }
+ : _Inherited(std::move<_Inherited>(__in)) { }
template<typename... _UElements>
tuple(const tuple<_UElements...>& __in)
- : _Inherited(__in) { }
+ : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in)) { }
template<typename... _UElements>
tuple(tuple<_UElements...>&& __in)
- : _Inherited(std::move(__in)) { }
+ : _Inherited(std::move<_Tuple_impl<0, _UElements...> >(__in)) { }
tuple&
operator=(const tuple& __in)
@@ -288,33 +290,27 @@
: _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
tuple(const tuple& __in)
- : _Inherited(__in) { }
+ : _Inherited(static_cast<const _Inherited&>(__in)) { }
tuple(tuple&& __in)
- : _Inherited(std::move(__in)) { }
+ : _Inherited(std::move<_Inherited>(__in)) { }
template<typename _U1, typename _U2>
tuple(const tuple<_U1, _U2>& __in)
- : _Inherited(__in) { }
+ : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
template<typename _U1, typename _U2>
tuple(tuple<_U1, _U2>&& __in)
- : _Inherited(std::move(__in)) { }
+ : _Inherited(std::move<_Tuple_impl<0, _U1, _U2> >(__in)) { }
template<typename _U1, typename _U2>
tuple(const pair<_U1, _U2>& __in)
- : _Inherited(_Tuple_impl<0,
- typename __add_c_ref<_U1>::type,
- typename __add_c_ref<_U2>::type>(__in.first,
- __in.second))
+ : _Inherited(__in.first, __in.second)
{ }
template<typename _U1, typename _U2>
tuple(pair<_U1, _U2>&& __in)
- : _Inherited(_Tuple_impl<0,
- typename std::remove_reference<_U1>::type&&,
- typename std::remove_reference<_U2>::type&&>
- (std::move(__in.first), std::move(__in.second)))
+ : _Inherited(std::forward<_U1>(__in.first), std::forward<_U2>(__in.second))
{ }
tuple&