On 10/04/2021 21:36, Keith Thompson wrote:
> Juha Nieminen <nos...@thanks.invalid> writes:
>> Paavo Helde <
myfir...@osa.pri.ee> wrote:
>>> You don't need scanf() to get incorrect numbers, you can easily have
>>> them also with C++ streams (a design bug IMO, stream exceptions should
>>> be on by default, but that's just me):
>>
>> But the thing is, std::sscanf() is probably going to be more efficient
>> than std::istringstream. Notice in your very own example:
>>
>>> const char* buffer = "12345678912345678";
>>> std::istringstream is(buffer);
>>
>> That's probably going to cause an extra memory allocation and
>> deallocation (not to talk about the needless copying of the string
>> contents into the allocated memory).
>>
>> I know that the problem may be not as bad when reading directly
>> from a file or standard input, but notice how in this particular
>> case, when parsing a value from a string in memory, C++ doesn't
>> really offer a good alternative. C++17 did introduce the
>> std::from_chars() family of functions that are designed to
>> read integers and floating point values from strings as
>> efficiently as possible (probably even surpassing the efficiency
>> of std::sscanf()), in-place, without any allocations nor copying,
>> which is a great addition to the standard library, but they can only
>> be used to parse one single value at a time.
>
> There's always strtol() and friends.
sscanf with type safe conversions and no annoying format specifiers. I