Topic: Is the 'as if' rule appliable here?
Author: gennaro_prota@yahoo.com (Gennaro Prota)
Date: Fri, 23 Jan 2004 23:17:10 +0000 (UTC) Raw View
Hi everybody,
I'm not sure whether to file a DR for overspecification, here. Can
anyone enlighten me?
The description of
template <class charT, class traits, size_t N>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>&, bitset<N>&);
in the standard (23.3.5) seems to mandate the use of std::string for
its implementation. Now, not only it would be possible to use e.g. a
std::vector but also not using any intermediate data structure at all,
because the maximum number of characters that can be extracted is
known: so one can just set/reset bits into the bitset starting from
position N - 1 and then shift the bitset itself to the right if less
than N characters are extracted. Example (completely untested):
template <class Ch, class Tr, size_t N>
basic_istream<Ch, Tr>&
operator>>(basic_istream<Ch, Tr>& is, bitset<N>& x)
{
ios_base::iostate err = ios_base::goodbit;
// in accordance with prop. resol. of lib dr 303.
//
const Ch zero = is.widen('0');
const Ch one = is.widen('1');
typename basic_istream<Ch, Tr>::sentry iprefix(is);
size_t stored = 0;
try {
if (iprefix) {
basic_streambuf <Ch, Tr> * buf = is.rdbuf();
typename Tr::int_type c = buf->sgetc();
for( ; stored < N; c = buf->snextc() ) {
if (Tr::eq_int_type(Tr::eof(), c)) {
err |= ios_base::eofbit;
break;
}
else {
const Ch to_c = Tr::to_char_type(c);
const bool is_one = Tr::eq(to_c, one);
if (!is_one && !Tr::eq(to_c, zero))
break; // non digit character
++stored;
x.set(N - stored, is_one);
}
} // for
if (stored == 0)
err |= ios_base::failbit;
else
x >>= (N - stored);
}
} catch (...) {
x >>= (N - stored);
bool rethrow_original = false;
try { is.setstate(ios_base::failbit); }
catch(...) { rethrow_original = true; }
if (rethrow_original)
throw;
}
if (err != ios_base::goodbit)
is.setstate (err);
return is;
}
Is such an implementation allowed under the 'as if' rule? Note that
the user could replace the global operator new and have, in that case,
different observable behaviors whether std::string is used or not.
Thoughts?
Genny.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]