Topic: bitset string constructor


Author: "Jonathan H Lundquist" <fluxsmith@fluxsmith.com>
Date: 1999/01/17
Raw View
Any comments on my comment below?  Am I currect in interpreting the standard
to require this verification of the string beyond the length actually used?

   template<typename T, typename Traits, typename Allocator>
explicit bitset(const basic_string<T, Traits, Allocator>& str,
              typename basic_string<T, Traits, Allocator>::size_type pos =
0,
              typename basic_string<T, Traits, Allocator>::size_type n =
                           basic_string<T, Traits, Allocator>::npos)
    : base()
   {
   if ( pos > str.size() )
      throw out_of_range("bitset");

   typename basic_string<T, Traits, Allocator>::size_type rlen = min(n,
str.size() - pos);
   n = min(N, rlen);
   typename basic_string<T, Traits, Allocator>::size_type i;
   for ( i = 0; i < n; ++i ) {
      switch ( str[pos + n - i - 1] ) {
      case '0':
         break;
      case '1':
         set(i);
         break;
      default:
         throw invalid_argument("bitset");
      }
   }

   // this seems silly, but this is what the standard says:
   for ( ; i < rlen; ++i ) {
      switch ( str[i] ) {
      case '0':
      case '1':
         break;
      default:
         throw invalid_argument("bitset");
      }
   }
   }




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]