What the purpose of imbue in C++?

后端 未结 2 518
猫巷女王i
猫巷女王i 2020-12-31 07:38

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

extern std::locale g_classicLocale;
class StringStream : public virtual std::ostringstream
{
 public:
        S         


        
相关标签:
2条回答
  • 2020-12-31 07:55

    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 ,.

    0 讨论(0)
  • 2020-12-31 07:58

    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).

    0 讨论(0)
提交回复
热议问题