money_get<> facet uses moneypunct's neg_format()

The money_get<> facet uses the moneypunct<> facet's neg_format() member, to determine the pattern for parsing the I/P monetary value.

This is rather strange.

Shouldn't pos_format() have been used for positive amounts and neg_format() for negative amounts?
How would it know the amount was positive without parsing it first?

I suppose it's true that if neg_format parser is not compatible with pos-format text, positive values won't round-trip through text form. Made a note on cppreference for pos_format.
Last edited on
How would it know the amount was positive without parsing it first?

True.

Made a note on cppreference for pos_format.

I think the issue may be a design issue rather than an implementation issue.

Perhaps, the moneypunct<> facet should have had 3 members:

1 and 2) pos_format() and neg_format() would be used by money_put<>.put().

3) get_format() would be used by money_get<>.get().

WIth the existing design, it looks like a member (neg_format()) is being used for something it wasn't meant for (going by the name).
Last edited on
> Perhaps, the moneypunct<> facet should have had 3 members:
> WIth the existing design, it looks like a member (neg_format()) is being used for something it wasn't meant for

A plausible approach might have been:
if( the input matches neg_format ) extract a negative monetary amount.
else if( the input matches pos_format ) extract a positive monetary amount.
else put the stream into the appropriate failed/eof state.

However, this is not really feasible, as per the current specification;
std::istreambuf_iterator<> (the default specified by the standard) is a single-pass input iterator.
Registered users can post here. Sign in or register to post.