Print additional info when various out-of-range conditions are detected.
[gcc.git] / libstdc++-v3 / include / std / bitset
index 1da6baf332f9f503836dc919fdd4f0296a9e36c3..708a434af9e178afda26e2136abcfce5a443b214 100644 (file)
@@ -752,6 +752,26 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
       typedef unsigned long _WordT;
 
+      template<class _CharT, class _Traits, class _Alloc>
+      void
+      _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
+                               size_t __position) const
+      {
+       if (__position > __s.size())
+         __throw_out_of_range_fmt(__N("bitset::bitset: __position "
+                                      "(which is %zu) > __s.size() "
+                                      "(which is %zu)"),
+                                  __position, __s.size());
+      }
+
+      void _M_check(size_t __position, const char *__s) const
+      {
+       if (__position >= _Nb)
+         __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
+                                      ">= _Nb (which is %zu)"),
+                                  __s, __position, _Nb);
+      }
+
       void
       _M_do_sanitize() _GLIBCXX_NOEXCEPT
       { 
@@ -867,9 +887,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
               size_t __position = 0)
        : _Base()
        {
-         if (__position > __s.size())
-           __throw_out_of_range(__N("bitset::bitset initial position "
-                                    "not valid"));
+         _M_check_initial_position(__s, __position);
          _M_copy_from_string(__s, __position,
                              std::basic_string<_CharT, _Traits, _Alloc>::npos,
                              _CharT('0'), _CharT('1'));
@@ -890,9 +908,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
               size_t __position, size_t __n)
        : _Base()
        {
-         if (__position > __s.size())
-           __throw_out_of_range(__N("bitset::bitset initial position "
-                                    "not valid"));
+         _M_check_initial_position(__s, __position);
          _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
        }
 
@@ -904,9 +920,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
               _CharT __zero, _CharT __one = _CharT('1'))
        : _Base()
        {
-         if (__position > __s.size())
-           __throw_out_of_range(__N("bitset::bitset initial position "
-                                    "not valid"));
+         _M_check_initial_position(__s, __position);
          _M_copy_from_string(__s, __position, __n, __zero, __one);
        }
 
@@ -1067,8 +1081,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       bitset<_Nb>&
       set(size_t __position, bool __val = true)
       {
-       if (__position >= _Nb)
-         __throw_out_of_range(__N("bitset::set"));
+       this->_M_check(__position, __N("bitset::set"));
        return _Unchecked_set(__position, __val);
       }
 
@@ -1092,8 +1105,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       bitset<_Nb>&
       reset(size_t __position)
       {
-       if (__position >= _Nb)
-         __throw_out_of_range(__N("bitset::reset"));
+       this->_M_check(__position, __N("bitset::reset"));
        return _Unchecked_reset(__position);
       }
       
@@ -1116,8 +1128,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       bitset<_Nb>&
       flip(size_t __position)
       {
-       if (__position >= _Nb)
-         __throw_out_of_range(__N("bitset::flip"));
+       this->_M_check(__position, __N("bitset::flip"));
        return _Unchecked_flip(__position);
       }
       
@@ -1302,8 +1313,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       bool
       test(size_t __position) const
       {
-       if (__position >= _Nb)
-         __throw_out_of_range(__N("bitset::test"));
+       this->_M_check(__position, __N("bitset::test"));
        return _Unchecked_test(__position);
       }