Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the purpose of imbue in C++?

Tags:

c++

I'm working with some code today, and I saw:

extern std::locale g_classicLocale;
class StringStream : public virtual std::ostringstream
{
 public:
        StringStream() { imbue(g_classicLocale); }
        virtual ~StringStream() {};
};

Then I came in face of imbue. What is the purpose of the imbue function in C++? What does it do? Are there any potential problems in using imbue (non-thread safe, memory allocation)?

like image 344
cybertextron Avatar asked Aug 06 '12 15:08

cybertextron


2 Answers

imbue is inherited by std::ostringstream from std::ios_base and it sets the locale of the stream to the specified locale.

This affects the way the stream prints (and reads) certain things; for instance, setting a French locale will cause the decimal point . to be replaced by ,.

like image 102
Praetorian Avatar answered Sep 19 '22 15:09

Praetorian


C++ streams perform their conversions to and from (numeric) types according to a locale, which is an object that summarizes all the localization information needed (decimal separator, date format, ...).

The default for streams is to use the current global locale, but you can set to a stream a custom locale using the imbue function, which is what your code does here - I suppose it's setting the default C locale to produce current locale-independent text (this is useful e.g. for serialization purposes).

like image 25
Matteo Italia Avatar answered Sep 21 '22 15:09

Matteo Italia