Boost logo

Boost Users :

From: Martin Adrian (adrianm_at_[hidden])
Date: 2006-02-22 16:04:30


> I'm probably not the first one to ask. But why is that behavior?

It is the standard way to implement facets.

The locale system keep a reference count for each facet and deletes them when
the last locale uses it is destroyed.

As with other facets it is possible to override this behaviour by passing 1 as
the last parameter "ref" to the constructor. It should works fine for
temporary stringstreams but I wouldn't use it for cout or in the global locale.

Almost all locale operations allocate heap memory so one more allocation on
facet creation makes a very small difference.

There is one theoretical memory leak problem if an exception occurs between
the allocation and the time when the facet is added to a locale.

Note that the examples in the date_time library:

  // create date_facet and date_input_facet using all defaults
  date_facet* date_output = new date_facet();
  ss.imbue(locale(ss.getloc(), date_output));

  // replace names in the output facet
  date_output->long_month_names(long_months);
  date_output->long_weekday_names(long_days);

isn't "standard" since the facets are meant to be immutable once they are
added to a locale. The streams are allowed to cache the values returned by the
facets so changes are not guaranteed to be effective until the next imbue call.
However, the date_time streaming is implemented without caches so it works.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net