Bug 79241 - std::locale constructor does not check environment
std::locale constructor does not check environment
Status: CLOSED UPSTREAM
Product: Red Hat Linux
Classification: Retired
Component: libstdc++ (Show other bugs)
8.0
i686 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2002-12-08 06:17 EST by Need Real Name
Modified: 2007-04-18 12:48 EDT (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2004-10-01 10:59:46 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:


Attachments (Terms of Use)

  None (edit)
Description Need Real Name 2002-12-08 06:18:00 EST
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 10:59:46 EDT
Fixed in 3.3/3.4 toolchains. Ie, FC1/FC2/RHEL 3/ RHEL 4.

-benjamin

Note You need to log in before you can comment on or make changes to this bug.