[RFC] Money_get::do_get appends?!?

Paolo Carlini pcarlini@unitus.it
Sun Feb 3 12:39:00 GMT 2002


Hi,

while working on libstdc++/5579 I noticed a strange behaviour. Consider this
testcase:

//////////////////////
#include <sstream>
#include <locale>
#include <iostream>
#include <cassert>

int main ()
{
  using namespace std;

  typedef istreambuf_iterator<char> InIt;
  InIt iend1, iend2;

  locale loc;
  string buffer1("123");
  string buffer2("456");
  string val;

  ios_base::iostate err;

  const money_get<char,InIt>& mg =
    use_facet<money_get<char, InIt> >(loc);

  istringstream fmt1(buffer1);
  InIt ibeg1(fmt1);
  mg.get(ibeg1,iend1,false,fmt1,err,val);
  std::cout << val << std::endl;
  assert( val == "123" );

  istringstream fmt2(buffer2);
  InIt ibeg2(fmt2);
  mg.get(ibeg2,iend2,false,fmt2,err,val);
  std::cout << val << std::endl;
  assert( val == "456" );
}
/////////////////

It outputs:
123
123456
a.out: testcase.cc:33: int main(): Assertion `val == "456"' failed.
Abort

The explanation is simple: money_get::do_get always *appends* to the string
__units passed by reference. Therefore, the second time it starts from "123" and
appends "456".

In my reading of the standard this is not correct.

Right now, I don't really have the time to fully test a patch (and I would like
to sort out 5579 before). Would be acceptable a fix simply calling
__units.erase() at the beginning of money_get::do_get??

Thanks,
Paolo.




More information about the Libstdc++ mailing list