C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: IOStreams behavior definition
From: Peter C++ (peter.cpp_at_[hidden])
Date: 2020-07-01 01:22:38


Dear Philip

If you want a specific parsing behavior, i suggest you implement your own type wrapping the target value type and and overload operator<< for your type with your desired behavior, i.e., taking the width field into account, that the built-ins do not seem to follow for int.

Input parsing is a topic that in general should not just use any standard facilities, that is true even in C where scanf is a function not recommended to be used in production systems, because of too many issues. Usually one would parse into strings checking for syntactic validity and then apply a conversion function from said syntactically correct string. However, what i am writing here, belongs into stack overflow and not about discussion of the C++ standard.

Regards
Peter

Sent from Peter Sommerlad's iPad
+41 79 432 23 32

> On 30 Jun 2020, at 23:46, Philip Prindeville via Std-Discussion <std-discussion_at_[hidden]> wrote:
>
> 
>
>> On Jun 30, 2020, at 11:46 AM, Ronan Keryell <rkeryell_at_[hidden]> wrote:
>>
>> […]
>>> http://www.cplusplus.com/reference/iomanip/setw/
>>>
>>> Refers to both input and output streams but only describes output behavior concretely.
>>>
>>> http://www.cplusplus.com/reference/ios/ios_base/width/
>>>
>>> Its homologue width() on the other hand makes no mention of input at all.
>>>
>>> Can some clarity be shed on this?
>>
>> We cannot clarify all the websites talking about C++ on the Internet. :-)
>>
>> This mailing-list is about discussing the ISO C++ standard itself more
>> than answering end-user questions. Perhaps places like
>> https://stackoverflow.com/questions/tagged/c%2b%2b are more responsive
>> for your question.
>>
>> Personally I find http://www.cplusplus.com quite obsolete compared to
>> https://en.cppreference.com/w/ in the same domain. Perhaps it might
>> answer your question if you dive into it.
>>
>
>
> Looking at:
>
> https://en.cppreference.com/w/cpp/io/manip/setw
>
> It claims that:
>
> When used in an expression out << setw(n) or in >> setw(n), sets the width parameter of the stream out or in to exactly n.
>
> But that doesn't seem to be the case for integer extractors. I think someone else called out that only std::string honors setw() on input, which seems arbitrary and violates the "Principle of Least Astonishment", or at least the expectation that object models & patterns get used uniformly and consistently.
>
> Looking at:
>
> https://en.cppreference.com/w/cpp/io/ios_base/width
>
> Manages the minimum number of characters to generate on certain output operations and the maximum number of characters to generate on certain input operations.
>
> By contrast mentions ONLY output, and omits any mention of input but then goes on to say
>
> Notes
> Some I/O functions call width(0) before returning, see std::setw (this results in this field having effect on the next I/O function only, and not on any subsequent I/O)
>
> The exact effects this modifier has on the input and output vary between the individual I/O functions and are described at each operator<< and operator>> overload page individually.
>
> And yes, on that note of "discussing the ISO C++ standard itself" I find guidance on uniformly implementing setw() on input streams to be lacking. Maybe it's worth fleshing out?
>
> -Philip
>
>
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion


STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups