C++ streambuf::pubimbue() function



The C++ std::streambuf::pubimbue() function is used to set a new locale for a stream buffer. when this function is invoked, it replaces the current locale of the stream buffer with the new one provided as an argument.

This function is primarily used by streams, such as std::istream or std::ostream, to manage locale-sensitive operations like formatting numbers or dates.

Syntax

Following is the syntax for std::streambuf::pubimbue() function.

locale pubimbue(const locale& _Loc);

Parameters

  • loc − It indicates the reference to a locale.

Return Value

This function returns the previous value stored in the locale object.

Exceptions

If an exception is thrown, the stream buffer is in a valid state.

Data races

It modifies the stream buffer object.

Example 1

In the following example, we are going to consider the basic usage of the pubimbue() function.

#include <iostream>
#include <sstream>
#include <locale>
int main() {
   std::ostringstream a;
   std::locale b("");
   a.rdbuf() -> pubimbue(b);
   a << 11128932.342;
   std::cout << "Result : " << a.str() << std::endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

Result : 1.11289e+07
streambuf.htm
Advertisements