diff options
author | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-08-29 16:11:19 +0000 |
---|---|---|
committer | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-08-29 16:11:19 +0000 |
commit | e227b1595da9af86bad23b9bea2cd01d32c4723f (patch) | |
tree | 2b1fe6e112b8d923fc0d3d94777e5e410738eee2 /libstdc++-v3 | |
parent | 613e3755d233773ed7a9e3143ccc84b85624aa51 (diff) | |
download | gcc-e227b1595da9af86bad23b9bea2cd01d32c4723f.tar.gz |
2005-08-29 Paolo Carlini <pcarlini@suse.de>
PR libstdc++/23578 (DR 464 [Ready])
* include/bits/stl_map.h (class map): Add at(const key_type&)
member functions.
* include/bits/stl_vector.h (class vector): Add data() member
functions.
* include/debug/map.h (class map): Adjust consistently.
* include/debug/vector (class vector): Likewise.
* testsuite/23_containers/map/element_access/1.cc: New.
* testsuite/23_containers/vector/data_access/1.cc: Likewise.
* docs/html/ext/howto.html: Add an entry for DR 464.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@103609 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libstdc++-v3')
-rw-r--r-- | libstdc++-v3/ChangeLog | 13 | ||||
-rw-r--r-- | libstdc++-v3/docs/html/ext/howto.html | 7 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/stl_map.h | 28 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/stl_vector.h | 15 | ||||
-rw-r--r-- | libstdc++-v3/include/debug/map.h | 4 | ||||
-rw-r--r-- | libstdc++-v3/include/debug/vector | 4 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/23_containers/map/element_access/1.cc | 79 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/23_containers/vector/data_access/1.cc | 51 |
8 files changed, 201 insertions, 0 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 76c62a1c471..a69c4ce46fb 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,16 @@ +2005-08-29 Paolo Carlini <pcarlini@suse.de> + + PR libstdc++/23578 (DR 464 [Ready]) + * include/bits/stl_map.h (class map): Add at(const key_type&) + member functions. + * include/bits/stl_vector.h (class vector): Add data() member + functions. + * include/debug/map.h (class map): Adjust consistently. + * include/debug/vector (class vector): Likewise. + * testsuite/23_containers/map/element_access/1.cc: New. + * testsuite/23_containers/vector/data_access/1.cc: Likewise. + * docs/html/ext/howto.html: Add an entry for DR 464. + 2005-08-26 Benjamin Kosnik <bkoz@redhat.com> PR libstdc++/20534 (contd) diff --git a/libstdc++-v3/docs/html/ext/howto.html b/libstdc++-v3/docs/html/ext/howto.html index 0b7b17b59de..85161964e5f 100644 --- a/libstdc++-v3/docs/html/ext/howto.html +++ b/libstdc++-v3/docs/html/ext/howto.html @@ -520,6 +520,13 @@ </dt> <dd>Don't fail if the next pointer is null and newoff is zero. </dd> + + <dt><a href="lwg-active.html#464">464</a>: + <em>Suggestion for new member functions in standard containers</em> + </dt> + <dd>Add <code>data()</code> to <code>std::vector</code> and + <code>at(const key_type&)</code> to <code>std::map</code>. + </dd> <!-- <dt><a href="lwg-defects.html#"></a>: <em></em> diff --git a/libstdc++-v3/include/bits/stl_map.h b/libstdc++-v3/include/bits/stl_map.h index c06560959f2..81bfc251ffa 100644 --- a/libstdc++-v3/include/bits/stl_map.h +++ b/libstdc++-v3/include/bits/stl_map.h @@ -61,6 +61,7 @@ #ifndef _MAP_H #define _MAP_H 1 +#include <bits/functexcept.h> #include <bits/concept_check.h> namespace _GLIBCXX_STD @@ -348,6 +349,33 @@ namespace _GLIBCXX_STD return (*__i).second; } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // DR 464. Suggestion for new member functions in standard containers. + /** + * @brief Access to %map data. + * @param k The key for which data should be retrieved. + * @return A reference to the data whose key is equivalent to k, if + * such a data is present in the map. + * @throw std::out_of_range If no such data is present. + */ + mapped_type& + at(const key_type& __k) + { + iterator __i = lower_bound(__k); + if (__i == end() || key_comp()(__k, (*__i).first)) + __throw_out_of_range(__N("map::at")); + return (*__i).second; + } + + const mapped_type& + at(const key_type& __k) const + { + const_iterator __i = lower_bound(__k); + if (__i == end() || key_comp()(__k, (*__i).first)) + __throw_out_of_range(__N("map::at")); + return (*__i).second; + } + // modifiers /** * @brief Attempts to insert a std::pair into the %map. diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h index 1a6dddcf7d2..94acec2fb2c 100644 --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -568,6 +568,21 @@ namespace _GLIBCXX_STD back() const { return *(end() - 1); } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // DR 464. Suggestion for new member functions in standard containers. + // data access + /** + * Returns a pointer such that [data(), data() + size()) is a valid + * range. For a non-empty %vector, data() == &front(). + */ + pointer + data() + { return pointer(this->_M_impl._M_start); } + + const_pointer + data() const + { return const_pointer(this->_M_impl._M_start); } + // [23.2.4.3] modifiers /** * @brief Add data to the end of the %vector. diff --git a/libstdc++-v3/include/debug/map.h b/libstdc++-v3/include/debug/map.h index caef350155b..e0722db6c4f 100644 --- a/libstdc++-v3/include/debug/map.h +++ b/libstdc++-v3/include/debug/map.h @@ -142,6 +142,10 @@ namespace __gnu_debug_def // 23.3.1.2 element access: using _Base::operator[]; + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // DR 464. Suggestion for new member functions in standard containers. + using _Base::at; + // modifiers: std::pair<iterator, bool> insert(const value_type& __x) diff --git a/libstdc++-v3/include/debug/vector b/libstdc++-v3/include/debug/vector index 0d62bd76db4..f2b36182423 100644 --- a/libstdc++-v3/include/debug/vector +++ b/libstdc++-v3/include/debug/vector @@ -230,6 +230,10 @@ namespace __gnu_debug_def return _Base::back(); } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // DR 464. Suggestion for new member functions in standard containers. + using _Base::data; + // 23.2.4.3 modifiers: void push_back(const _Tp& __x) diff --git a/libstdc++-v3/testsuite/23_containers/map/element_access/1.cc b/libstdc++-v3/testsuite/23_containers/map/element_access/1.cc new file mode 100644 index 00000000000..5bc464aafdd --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/map/element_access/1.cc @@ -0,0 +1,79 @@ +// 2005-08-29 Paolo Carlini <pcarlini@suse.de> +// +// Copyright (C) 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include <map> +#include <stdexcept> +#include <testsuite_hooks.h> + +// libstdc++/23578 +void test01() +{ + bool test __attribute__((unused)) = true; + typedef std::map<int, double> map_type; + + { + map_type m; + m[0] = 1.5; + + double& rd = m.at(0); + VERIFY( rd == 1.5 ); + try + { + m.at(1); + } + catch(std::out_of_range& obj) + { + // Expected. + } + catch(...) + { + // Failed. + throw; + } + } + + { + map_type m; + m[1] = 2.5; + const map_type cm(m); + + const double& crd = cm.at(1); + VERIFY( crd == 2.5 ); + try + { + cm.at(0); + } + catch(std::out_of_range& obj) + { + // Expected. + } + catch(...) + { + // Failed. + throw; + } + } +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/23_containers/vector/data_access/1.cc b/libstdc++-v3/testsuite/23_containers/vector/data_access/1.cc new file mode 100644 index 00000000000..41d5819aee8 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/vector/data_access/1.cc @@ -0,0 +1,51 @@ +// 2005-08-29 Paolo Carlini <pcarlini@suse.de> +// +// Copyright (C) 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +// USA. + +#include <vector> +#include <testsuite_hooks.h> + +// libstdc++/23578 +void test01() +{ + bool test __attribute__((unused)) = true; + typedef std::vector<int> vector_type; + + { + const int A[] = { 0, 1, 2, 3, 4 }; + vector_type v(A, A + 5); + VERIFY( v.data() == &v.front() ); + int* pi = v.data(); + VERIFY( *pi == 0 ); + } + + { + const int A[] = { 4, 3, 2, 1, 0 }; + const vector_type cv(A, A + 5); + VERIFY( cv.data() == &cv.front() ); + const int* pci = cv.data(); + VERIFY( *pci == 4 ); + } +} + +int main() +{ + test01(); + return 0; +} |