nyarlathotep108
nyarlathotep108

Reputation: 5521

What is the standard default localization of ostream?

I am using CLang 8.0 and given this code example:

#include <locale>
#include <sstream>
#include <iostream>

struct slash : std::numpunct<char> {
    char do_decimal_point()   const { return '/'; }  // separate with slash
};

int main()
{
    std::ostringstream oss;

    auto slash_locale = std::locale(std::cout.getloc(), new slash);
    std::locale::global( slash_locale );

    oss << 4.5;
    std::cout << oss.str();
}

What I get is "4.5". My question is: is this behaviour of ignoring the locale written in the standard? Because a colleague of mine is using XCode and stating that there "4/5" is being printed. I would like to understand if it is supposed to be implementation defined, or if the standard, in case my collegue states the truth, is being violated.

Upvotes: 1

Views: 291

Answers (1)

user7860670
user7860670

Reputation: 37578

Standard defines that during construction basic_stream base object of basic_stringbuf object is getting initialized with a copy of current global locale and that the results of using that locale will be in effect until member imbue is called.

30.6.3.1 basic_streambuf constructors [streambuf.cons]
basic_streambuf();
1 Effects: Constructs an object of class basic_streambuf<charT, traits> and initializes:
(1.1) all its pointer member objects to null pointers,
(1.2) the getloc() member to a copy the global locale, locale(), at the time of construction.
2 Remarks: Once the getloc() member is initialized, results of calling locale member functions, and of members of facets so obtained, can safely be cached until the next time the member imbue is called.

So in this case the effects of changing of global locale should be visible only if oss is created after that change took place.

Upvotes: 1

Related Questions