Commit e7457c3e by Paolo Carlini Committed by Paolo Carlini

array (array<>::_M_at): New.

2006-10-28  Paolo Carlini  <pcarlini@suse.de>
	
	* include/tr1/array (array<>::_M_at): New.
	(array<>::at): Fix off-by-one bug, use the above.
	* testsuite/tr1/6_containers/array/element_access/
	at_out_of_range.cc: Adjust.

	* include/tr1/array (class array<>): Remove non-conforming default
	for the second parameter.
	* include/ext/array_allocator.h: Adjust.

	* include/tr1/array (array<>::front, array<>::back): Do not return
	a reference to memory not belonging to the array when _Nm == 0.

From-SVN: r118114
parent f1827a8c
2006-10-28 Paolo Carlini <pcarlini@suse.de>
* include/tr1/array (array<>::_M_at): New.
(array<>::at): Fix off-by-one bug, use the above.
* testsuite/tr1/6_containers/array/element_access/
at_out_of_range.cc: Adjust.
* include/tr1/array (class array<>): Remove non-conforming default
for the second parameter.
* include/ext/array_allocator.h: Adjust.
* include/tr1/array (array<>::front, array<>::back): Do not return
a reference to memory not belonging to the array when _Nm == 0.
2006-10-17 Paolo Carlini <pcarlini@suse.de> 2006-10-17 Paolo Carlini <pcarlini@suse.de>
* include/bits/locale_facets.tcc (money_get<>::__do_get(iter_type, * include/bits/locale_facets.tcc (money_get<>::__do_get(iter_type,
......
...@@ -87,7 +87,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) ...@@ -87,7 +87,7 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
* @brief An allocator that uses previously allocated memory. * @brief An allocator that uses previously allocated memory.
* This memory can be externally, globally, or otherwise allocated. * This memory can be externally, globally, or otherwise allocated.
*/ */
template<typename _Tp, typename _Array = std::tr1::array<_Tp> > template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
class array_allocator : public array_allocator_base<_Tp> class array_allocator : public array_allocator_base<_Tp>
{ {
public: public:
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <bits/functexcept.h> #include <bits/functexcept.h>
#include <ext/type_traits.h>
//namespace std::tr1 //namespace std::tr1
namespace std namespace std
...@@ -47,7 +48,7 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1) ...@@ -47,7 +48,7 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
/// @brief struct array [6.2.2]. /// @brief struct array [6.2.2].
/// NB: Requires complete type _Tp. /// NB: Requires complete type _Tp.
template<typename _Tp, std::size_t _Nm = 1> template<typename _Tp, std::size_t _Nm>
struct array struct array
{ {
typedef _Tp value_type; typedef _Tp value_type;
...@@ -60,9 +61,6 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1) ...@@ -60,9 +61,6 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
// Compile time constant without other dependencies.
enum { _S_index = _Nm };
// Support for zero-sized arrays mandatory. // Support for zero-sized arrays mandatory.
value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__)); value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__));
...@@ -128,21 +126,13 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1) ...@@ -128,21 +126,13 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
operator[](size_type __n) const operator[](size_type __n) const
{ return _M_instance[__n]; } { return _M_instance[__n]; }
const_reference
at(size_type __n) const
{
if (__builtin_expect(__n > _Nm, false))
std::__throw_out_of_range("array::at");
return _M_instance[__n];
}
reference reference
at(size_type __n) at(size_type __n)
{ { return _M_at<_Nm>(__n); }
if (__builtin_expect(__n > _Nm, false))
std::__throw_out_of_range("array::at"); const_reference
return _M_instance[__n]; at(size_type __n) const
} { return _M_at<_Nm>(__n); }
reference reference
front() front()
...@@ -154,11 +144,11 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1) ...@@ -154,11 +144,11 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
reference reference
back() back()
{ return *(end() - 1); } { return _Nm ? *(end() - 1) : *end(); }
const_reference const_reference
back() const back() const
{ return *(end() - 1); } { return _Nm ? *(end() - 1) : *end(); }
_Tp* _Tp*
data() data()
...@@ -167,6 +157,42 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1) ...@@ -167,6 +157,42 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
const _Tp* const _Tp*
data() const data() const
{ return &_M_instance[0]; } { return &_M_instance[0]; }
private:
template<std::size_t _Mm>
typename __gnu_cxx::__enable_if<_Mm, reference>::__type
_M_at(size_type __n)
{
if (__builtin_expect(__n >= _Mm, false))
std::__throw_out_of_range("array::_M_at");
return _M_instance[__n];
}
// Avoid "unsigned comparison with zero" warnings.
template<std::size_t _Mm>
typename __gnu_cxx::__enable_if<!_Mm, reference>::__type
_M_at(size_type)
{
std::__throw_out_of_range("array::_M_at");
return _M_instance[0];
}
template<std::size_t _Mm>
typename __gnu_cxx::__enable_if<_Mm, const_reference>::__type
_M_at(size_type __n) const
{
if (__builtin_expect(__n >= _Mm, false))
std::__throw_out_of_range("array::_M_at");
return _M_instance[__n];
}
template<std::size_t _Mm>
typename __gnu_cxx::__enable_if<!_Mm, const_reference>::__type
_M_at(size_type) const
{
std::__throw_out_of_range("array::_M_at");
return _M_instance[0];
}
}; };
// Array comparisons. // Array comparisons.
......
// 2004-10-20 Benjamin Kosnik <bkoz@redhat.com> // 2004-10-20 Benjamin Kosnik <bkoz@redhat.com>
// //
// Copyright (C) 2004 Free Software Foundation, Inc. // Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <tr1/array> #include <tr1/array>
#include <stdexcept> #include <stdexcept>
#include <testsuite_hooks.h>
void void
test01() test01()
...@@ -34,15 +35,17 @@ test01() ...@@ -34,15 +35,17 @@ test01()
try try
{ {
a.at(len); a.at(len);
VERIFY( false );
} }
catch(std::out_of_range& obj) catch(std::out_of_range& obj)
{ {
// Expected. // Expected.
VERIFY( true );
} }
catch(...) catch(...)
{ {
// Failed. // Failed.
throw; VERIFY( false );
} }
} }
...@@ -51,4 +54,3 @@ int main() ...@@ -51,4 +54,3 @@ int main()
test01(); test01();
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment