Status of time_get and time_put facets
Martin Sebor
sebor@roguewave.com
Wed Jan 29 23:43:00 GMT 2003
Paolo Carlini wrote:
>
...
> Ok, thanks for your explanation and hints: I plan to investigate in
> detail the issue in the next days.
>
> For sure the current testsuite lacks testcases for the behaviour
> you expect...
Here's another test case that translates the name of a weekday
read from stdin in a locale given by the first argument to a
locale given by the second command line argument. It uses
a single locale object with time_get and time_put each from
a distinct named locale. Sample output is here:
$ echo "Mittwoch" | ./t de_DE fr_FR
mercredi
In order for this to work correctly, the stream object cannot
be used to retrieve the facet data. Each time facet has to have
its own.
With gcc/libstdc++ 3.2.1 it fails to compile due to a const
correctness bug (locale template ctor takes any Facet*, i.e.,
including const std::time_get<char>*). After fixing the
compilation error, the program segfaults.
Regards
Martin
#include <iostream>
#include <locale>
int main (int argc, const char *argv[])
{
const std::locale l1 (argc > 1 ? argv [1] : "");
const std::locale l2 (argc > 2 ? argv [2] : "");
const std::locale l1_l2 =
std::locale (std::locale (std::locale::classic (),
&std::use_facet<std::time_get<char>
>(l1)),
&std::use_facet<std::time_put<char> >(l2));
const std::time_get<char> &tg =
std::use_facet<std::time_get<char> >(l1_l2);
const std::time_put<char> &tp =
std::use_facet<std::time_put<char> >(l1_l2);
std::tm t = std::tm ();
std::ios::iostate err = std::ios::goodbit;
// benign but unnecessary
// std::cin.imbue (l1_l2);
// std::cout.imbue (l1_l2);
tg.get_weekday (std::cin.rdbuf (), std::istreambuf_iterator<char>(),
std::cin, err, &t);
if (err & std::ios::failbit)
return 1;
tp.put (std::cout.rdbuf (), std::cout, ' ', &t, 'A') = '\n';
}
More information about the Libstdc++
mailing list