// Internal policy header for TR1 unordered_set and unordered_map -*- C++ -*-
-// Copyright (C) 2007 Free Software Foundation, Inc.
+// Copyright (C) 2007, 2008 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
};
template<typename _Key, typename _Pair, typename _Hashtable>
- struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>
+ struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>
{
typedef typename _Pair::second_type mapped_type;
mapped_type&
operator[](const _Key& __k);
+
+#ifdef _GLIBCXX_INCLUDE_AS_CXX0X
+ // _GLIBCXX_RESOLVE_LIB_DEFECTS
+ // DR 761. unordered_map needs an at() member function.
+ mapped_type&
+ at(const _Key& __k);
+
+ const mapped_type&
+ at(const _Key& __k) const;
+#endif
};
template<typename _Key, typename _Pair, typename _Hashtable>
return (__p->_M_v).second;
}
+#ifdef _GLIBCXX_INCLUDE_AS_CXX0X
+ template<typename _Key, typename _Pair, typename _Hashtable>
+ typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
+ true, _Hashtable>::mapped_type&
+ _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
+ at(const _Key& __k)
+ {
+ _Hashtable* __h = static_cast<_Hashtable*>(this);
+ typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
+ std::size_t __n = __h->_M_bucket_index(__k, __code,
+ __h->_M_bucket_count);
+
+ typename _Hashtable::_Node* __p =
+ __h->_M_find_node(__h->_M_buckets[__n], __k, __code);
+ if (!__p)
+ __throw_out_of_range(__N("_Map_base::at"));
+ return (__p->_M_v).second;
+ }
+
+ template<typename _Key, typename _Pair, typename _Hashtable>
+ const typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
+ true, _Hashtable>::mapped_type&
+ _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
+ at(const _Key& __k) const
+ {
+ const _Hashtable* __h = static_cast<const _Hashtable*>(this);
+ typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
+ std::size_t __n = __h->_M_bucket_index(__k, __code,
+ __h->_M_bucket_count);
+
+ typename _Hashtable::_Node* __p =
+ __h->_M_find_node(__h->_M_buckets[__n], __k, __code);
+ if (!__p)
+ __throw_out_of_range(__N("_Map_base::at"));
+ return (__p->_M_v).second;
+ }
+#endif
+
// class template _Rehash_base. Give hashtable the max_load_factor
// functions iff the rehash policy is _Prime_rehash_policy.
template<typename _RehashPolicy, typename _Hashtable>
--- /dev/null
+// { dg-options "-std=gnu++0x" }
+// 2008-05-22 Paolo Carlini <paolo.carlini@oracle.com>
+//
+// Copyright (C) 2008 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 <unordered_map>
+#include <stdexcept>
+#include <testsuite_hooks.h>
+
+// DR 761. unordered_map needs an at() member function.
+void test01()
+{
+ bool test __attribute__((unused)) = true;
+ typedef std::unordered_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;
+}