On 20.07.2018 00:23, Alf P. Steinbach wrote:
> [snip]
> Because I also remember g++'s C++ level locale handling to be missing or
> screwed up, so that /could/ be the problem?
That was the problem.
Or rather /is/ the problem.
Not so nice to have all that inefficient locale stuff in the iostreams,
when actual use of it isn't portable to one of the main compilers. :(
-----------------------------------------------------------------------------
// Source encoding: utf-8 (π should be a greek "pi" char)
#include <iostream>
#include <locale>
#include <locale.h>
#include <stdexcept>
#include <stdlib.h>
auto main()
-> int
{
setlocale( LC_ALL, "" ); // Default national locale, for UTF-8
in Unix-land.
#ifdef PERIOD
setlocale( LC_NUMERIC, "C" ); // But periods as fraction
separators, please.
#endif
std::cout << "C level locale = \"" << setlocale( LC_ALL, nullptr )
<< "\"\n";
try
{
const auto& loc =
std::locale{ "" }
#ifdef PERIOD
.combine<std::numpunct<char>>( std::locale::classic() )
#endif
;
std::cout << "C++ level locale = \"" <<
loc.name() << "\"\n";
std::cout.imbue( loc );
printf( "printf blåbærsyltetøy, %g.\n", 3.14 );
std::cout << "cout blåbærsyltetøy, " << 3.14 << ".\n";
return EXIT_SUCCESS;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << "\n";
}
catch( ... )
{
std::cerr << "!!! non-standard exception\n";
}
return EXIT_FAILURE;
}
-----------------------------------------------------------------------------
Results with MinGW g++ 7.3.0 in Windows 10:
-----------------------------------------------------------------------------
[P:\temp]
> chcp
Active code page: 1252
[P:\temp]
> g++ locale-fix.cpp && a
C level locale = "Norwegian Bokmål_Svalbard and Jan Mayen.1252"
C++ level locale = "C"
printf blåbærsyltetøy, 3,14.
cout blåbærsyltetøy, 3.14.
[P:\temp]
> g++ locale-fix.cpp -D PERIOD && a
C level locale = "LC_COLLATE=Norwegian Bokmål_Svalbard and Jan
Mayen.1252;LC_CTYPE=Norwegian Bokmål_Svalbard and Jan
Mayen.1252;LC_MONETARY=Norwegian Bokmål_Svalbard and Jan
Mayen.1252;LC_NUMERIC=C;LC_TIME=Norwegian Bokmål_Svalbard and Jan
Mayen.1252"
C++ level locale = "C"
printf blåbærsyltetøy, 3.14.
cout blåbærsyltetøy, 3.14.
-----------------------------------------------------------------------------
The C++ level locale is unaffected by anything, it just remains as "C". :(
The code works with Visual C++.
Cheers & hope this info can be useful to someone, maybe,
- Alf