This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[Patch] DR 543
- From: Paolo Carlini <pcarlini at suse dot de>
- To: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Mon, 04 Dec 2006 16:45:11 +0100
- Subject: [Patch] DR 543
Hi,
the below is a straightforward implementation of the DR, which, after
all, is about a sensible user expectation: other implementations don't
crash on the testcase. As you can see, I had to disable the lazy memory
allocation in gslice() and implement trivially the proposed resolution:
believe me, I tried rather hard to keep it but I failed, ultimately
because of the const& nature of gslice_array<>::_M_index. I think it's
acceptable, for now, at least, but of course I'm open to other
suggestions, I'll wait until tomorrow, anyway.
Tested x86-linux.
Paolo.
////////////////
2006-12-04 Paolo Carlini <pcarlini@suse.de>
DR 543, [Ready].
* include/bits/slice_array.h (slice::slice()): Implement the
resolution.
* include/bits/gslice.h (gslice::_Indexer::_Indexer()): Add.
(gslice::gslice()): Use it.
(gslice::start, gslice::size, gslice::stride,
gslice::gslice(const gslice&), gslice::~gslice(),
gslice::operator=(const gslice&)): Adjust.
* testsuite/26_numerics/valarray/dr543.cc: New.
* docs/html/ext/howto.html: Add an entry for DR 543.
Index: include/bits/gslice.h
===================================================================
--- include/bits/gslice.h (revision 119482)
+++ include/bits/gslice.h (working copy)
@@ -1,6 +1,6 @@
// The template and inlines for the -*- C++ -*- gslice class.
-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2005
+// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2005, 2006
// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
@@ -64,7 +64,7 @@
{
public:
/// Construct an empty slice.
- gslice ();
+ gslice();
/**
* @brief Construct a slice.
@@ -108,8 +108,13 @@
valarray<size_t> _M_size;
valarray<size_t> _M_stride;
valarray<size_t> _M_index; // Linear array of referenced indices
+
+ _Indexer()
+ : _M_count(1), _M_start(0), _M_size(), _M_stride(), _M_index() {}
+
_Indexer(size_t, const valarray<size_t>&,
const valarray<size_t>&);
+
void
_M_increment_use()
{ ++_M_count; }
@@ -125,18 +130,22 @@
};
inline size_t
- gslice::start () const
- { return _M_index ? _M_index->_M_start : 0; }
+ gslice::start() const
+ { return _M_index->_M_start; }
inline valarray<size_t>
- gslice::size () const
- { return _M_index ? _M_index->_M_size : valarray<size_t>(); }
+ gslice::size() const
+ { return _M_index->_M_size; }
inline valarray<size_t>
- gslice::stride () const
- { return _M_index ? _M_index->_M_stride : valarray<size_t>(); }
+ gslice::stride() const
+ { return _M_index->_M_stride; }
- inline gslice::gslice () : _M_index(0) {}
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 543. valarray slice default constructor
+ inline
+ gslice::gslice()
+ : _M_index(new gslice::_Indexer()) {}
inline
gslice::gslice(size_t __o, const valarray<size_t>& __l,
@@ -144,22 +153,22 @@
: _M_index(new gslice::_Indexer(__o, __l, __s)) {}
inline
- gslice::gslice(const gslice& __g) : _M_index(__g._M_index)
- { if (_M_index) _M_index->_M_increment_use(); }
+ gslice::gslice(const gslice& __g)
+ : _M_index(__g._M_index)
+ { _M_index->_M_increment_use(); }
inline
gslice::~gslice()
{
- if (_M_index && _M_index->_M_decrement_use() == 0)
+ if (_M_index->_M_decrement_use() == 0)
delete _M_index;
}
inline gslice&
- gslice::operator= (const gslice& __g)
+ gslice::operator=(const gslice& __g)
{
- if (__g._M_index)
- __g._M_index->_M_increment_use();
- if (_M_index && _M_index->_M_decrement_use() == 0)
+ __g._M_index->_M_increment_use();
+ if (_M_index->_M_decrement_use() == 0)
delete _M_index;
_M_index = __g._M_index;
return *this;
Index: include/bits/slice_array.h
===================================================================
--- include/bits/slice_array.h (revision 119482)
+++ include/bits/slice_array.h (working copy)
@@ -1,6 +1,6 @@
// The template and inlines for the -*- C++ -*- slice_array class.
-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005
+// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006
// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
@@ -83,10 +83,11 @@
size_t _M_st; // stride unit
};
- // The default constructor constructor is not required to initialize
- // data members with any meaningful values, so we choose to do nothing.
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // 543. valarray slice default constructor
inline
- slice::slice() {}
+ slice::slice()
+ : _M_off(0), _M_sz(0), _M_st(0) {}
inline
slice::slice(size_t __o, size_t __d, size_t __s)
Index: docs/html/ext/howto.html
===================================================================
--- docs/html/ext/howto.html (revision 119482)
+++ docs/html/ext/howto.html (working copy)
@@ -594,6 +594,12 @@
input_iterator' value_type.
</dd>
+ <dt><a href="lwg-active.html#543">543</a>:
+ <em>valarray slice default constructor</em>
+ </dt>
+ <dd>Follow the straightforward proposed resolution.
+ </dd>
+
<dt><a href="lwg-active.html#586">586</a>:
<em>string inserter not a formatted function</em>
</dt>
Index: testsuite/26_numerics/valarray/dr543.cc
===================================================================
--- testsuite/26_numerics/valarray/dr543.cc (revision 0)
+++ testsuite/26_numerics/valarray/dr543.cc (revision 0)
@@ -0,0 +1,40 @@
+// Copyright (C) 2006 Free Software Foundation
+//
+// 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+#include <valarray>
+#include <testsuite_hooks.h>
+
+// DR 543. valarray slice default constructor
+void test01()
+{
+ bool test __attribute__((unused)) = true;
+
+ std::valarray<int> v1(10);
+ std::valarray<int> v2 = v1[std::slice()];
+ VERIFY( v2.size() == 0 );
+
+ std::valarray<int> v3(10);
+ std::valarray<int> v4 = v3[std::gslice()];
+ VERIFY( v4.size() == 0 );
+}
+
+int main()
+{
+ test01();
+ return 0;
+}