Topic: how does num_get parse hex constants?
Author: jksalmon <john.salmon@gmail.com>
Date: Sat, 5 Dec 2009 22:29:58 CST Raw View
I'm having trouble understanding how 22.2.2.1.2 "num_get virtual
function" allows conversion of sequences that start with the
characters
"0x". I.e., I think the following should be ok,
istringstream iss("0x1");
iss.setf(ios_base::hex, ios_base::basefield);
int one = 2;
iss >> one;
assert(one == 1);
assert(iss);
but I can't see how this is permitted by 22.2.2.1.2. In particular,
when we hit the 'x', it looks to me like the Stage 2 "as if" code
fragment sets c to '\0':
<snip>
Stage 2: If in==end then stage 2 terminates. Otherwise a charT
is taken from in and local variables are initialized as if by
char_type ct = *in ;
char c = src[find(atoms, atoms + sizeof(src) - 1, ct) - atoms];
if ( ct == use_facet<numpunct<charT> >(loc).decimal_point() )
c = '.';
bool discard =
( ct == use_facet<numpunct<charT> >(loc).thousands_sep()
&&
use_facet<numpunct<charT> >(loc).grouping().length() != 0 );
where the values src and atoms are defined as if by:
static const char src[] = "0123456789abcdefABCDEF+-";
char_type atoms[sizeof(src)];
use_facet<ctype<charT> >(loc).widen(src, src + sizeof(src),
atoms);
for this value of loc.
</snip>
Why? Because there's no 'x' in src, so the find "fails", returning
atoms+sizeof(src)-1, and hence c='\0'. Stage 2 should stop at this
point, just as it would if ct were 'y' or 'z', and the conversion
should be applied to the accumulated characters so far, setting i to 0
and leaving "x1" unprocessed.
But that's not what happens, and I don't think it's what anyone
intended. So what have I missed?
John
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: jksalmon <john.salmon@gmail.com>
Date: Mon, 7 Dec 2009 00:35:54 CST Raw View
On Dec 5, 11:29 pm, jksalmon <john.sal...@gmail.com> wrote:
> I'm having trouble understanding how 22.2.2.1.2 "num_get virtual
> function" allows conversion of sequences that start with the
> characters "0x".
...
> <snip>
> ...
> static const char src[] = "0123456789abcdefABCDEF+-";
> char_type atoms[sizeof(src)];
> use_facet<ctype<charT> >(loc).widen(src, src + sizeof(src),
> atoms);
>
> for this value of loc.
> </snip>
>
> Why? Because there's no 'x' in src, so the find "fails",
> ...
]
Aha! I see that this has changed in the Draft Standard, N2914-09-104
dated 2009-06-22. The letters 'X' and 'x' appear in the src string.
John
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]