Bug 79241

Summary: std::locale constructor does not check environment
Product: [Retired] Red Hat Linux Reporter: Need Real Name <peturr02>
Component: libstdc++Assignee: Jakub Jelinek <jakub>
Status: CLOSED UPSTREAM QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 8.0CC: mitr
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2004-10-01 14:59:46 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Need Real Name 2002-12-08 11:18:00 UTC
Description of Problem:
A std::locale constructed with an empty string ("") does not give the same 
results as calling setlocale(LC_ALL, "").

man setlocale says:

 If locale is "", each part of the locale that should be modified is set
 according  to the environment variables. The details are implementation
 dependent.  For glibc, first (regardless of category), the  environment
 variable  LC_ALL  is  inspected, next the environment variable with the
 same name as the category (LC_COLLATE, LC_CTYPE, LC_MESSAGES,
 LC_MONETARY,  LC_NUMERIC,  LC_TIME) and finally the environment variable LANG.

The same should apply to std::locale, that is, if LC_ALL is set,
 std::locale loc ("");
should be equivalent to
 std::locale loc (getenv("LC_ALL"));
but it appears to work as
 std::locale loc ("C");

Version-Release number of selected component (if applicable):
libstdc++-3.2-7.i386.rpm

How Reproducible:
Always

Steps to Reproduce:
This program demonstrates the problem:

#include <cstdlib>
#include <string>
#include <locale>
#include <cassert>
#include <cstdio>
#include <sstream>
#include <iomanip>
using namespace std;

int main(int argc, char** argv)
{
	clearenv();

	ostringstream stream;
	stream << fixed << setprecision(1);

	stream << 3.5;
	assert(stream.str() == "3.5");

	stream.str("");
	stream.clear();
	stream.imbue(locale("is_IS"));
	stream << 3.5;
	assert(stream.str() == "3,5");

	if (setenv("LC_ALL", "is_IS", true) || setenv("LANG", "is_IS", true))
	{
		perror(argv[0]);
		return -1;
	}

	stream.str("");
	stream.clear();
	stream.imbue(locale(""));
	stream << 3.5;
	assert(stream.str() == "3,5"); // Fails, stream.str() == "3.5"

	return 0;
}

Actual Results:


Expected Results:


Additional Information:

Comment 1 Benjamin Kosnik 2004-10-01 14:59:46 UTC
Fixed in 3.3/3.4 toolchains. Ie, FC1/FC2/RHEL 3/ RHEL 4.

-benjamin