[cfe-dev] istringstream possibly not parsing doubles correctly

Charles Reilly via cfe-dev cfe-dev at lists.llvm.org
Sun Jul 15 04:19:46 PDT 2018


Consider this code:

std::istringstream input("123A"); double d; input>>d;

Compiled with gcc or cl (and their usual standard libraries) it reads 
123 into d and leaves the input position on the 'A'.

With libc++ on OS X [Apple LLVM version 9.0.0 (clang-900.0.39.2)] it 
doesn't read anything and leaves the input position at the start.

On the other hand, all platforms read the number if the double d is 
replaced with an int.

In the n4659 draft of C++17, ยง25.4.2.1.2 num_get virtual functions, 
Stage 2, seems to be the relevant section.  Most of it is about 
character encodings and thousands separators; the key part is "...a 
check is made to determine if c is allowed as the next character of an 
input field of the conversion specifier returned by Stage 1. If so, it 
is accumulated."  This seems to describe the gcc/cl behaviour.

Cheers,
Charles Reilly



More information about the cfe-dev mailing list