stream's locale not getting imbued to POSIX

A stream's locale defaults to the classic "C" locale.

I'm trying to change it to "POSIX", but it remains as "C".

There is no run-time error, which means that both locales are supported, but the locale doesn't change.

Is this because the "POSIX" locale is internally implemented by the "C" locale?

The compiler is g++ 6.3.

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// ...

template<typename S>
void StreamLocaleName(S& s,
                      string m)
{
    /// Get the stream's locale.
    locale loc = s.rdbuf()->getloc();

    /// Display locale name.
    cout << m << "'s locale: "
         << loc.name() << endl;
}


int main()
{
   string s {};
   istringstream iss {s};

   /// Display the istringstream's initial locale name.
   StreamLocaleName(iss, "iss");

   /// Set the istringstream's locale to POSIX.
   locale posix {"POSIX"};
   iss.imbue(posix);

   /// Display the istringstream's new locale name.
   StreamLocaleName(iss, "iss");
}


https://ideone.com/6FX2sd

Output:


iss's locale: C
iss's locale: C
Simpler demo:
1
2
3
4
5
6
#include <locale>
#include <iostream>
int main()
{
   std::cout << std::locale{"POSIX"}.name() << '\n';
}

gcc/libstdc++ prints "C": https://wandbox.org/permlink/15L6yObrvxetjOst
clang/libc++ prints "POSIX": https://wandbox.org/permlink/GE48QmgDT4mKMsB9

looking at POSIX specification, http://pubs.opengroup.org/onlinepubs/9699919799/functions/setlocale.html
posix wrote:
"POSIX"
Specifies the minimal environment for C-language translation called the POSIX locale. The POSIX locale is the default global locale at entry to main().
"C"
Equivalent to "POSIX".

Looks like different implementations chose different ways to represent that "equivalence". Note the C standard says "C" is the default global locale at entry to main(). I think both approaches are defensible.
Last edited on
You are right. Thanks.
It's true that on wandbox, the POSIX locale's name is displayed as POSIX.

 
   std::cout << std::locale{"POSIX"}.name() << '\n';


However, when I run my original program even on wandbox, the name of the locale is still returned as "C", even after the stream is imbued with the POSIX locale!

https://wandbox.org/permlink/dm6tCD1KKtd7hSTr


iss's locale: C
iss's locale: C

it matches what my demo showed. With GCC it prints C, with clang it prints POSIX: https://wandbox.org/permlink/yyRA6oTelMlneFIx
Last edited on
You are absolutely right.

I had inadvertently continued to use the (default) setting for the C++ compiler (which is g++). Hence the error.

Change the compiler to clang and it works as expected.

Thanks.
Registered users can post here. Sign in or register to post.