 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Siemel Naran Guest
|
Posted: Thu Jun 26, 2003 2:22 pm Post subject: Re: How do I output 1000000 to 1,000,000 |
|
|
"Anthony Williams" <anthony.williamsNOSPAM (AT) anthonyw (DOT) cjb.net> wrote in
message
| Quote: | std::locale myLocale(std::locale(),new MyNumPunctFacet);
|
This seems exception unsafe. What if you create the new MyNumPunctFacet
successfully, then in std::locale() the system throws, then the new
facet is
not destroyed?
The good news is that on Borland 6 the program works.
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
MiniDisc_2k2 Guest
|
Posted: Thu Jun 26, 2003 7:39 pm Post subject: Re: How do I output 1000000 to 1,000,000 |
|
|
"Siemel Naran" <SiemelNaran (AT) KILL (DOT) att.net> wrote
| Quote: | "Anthony Williams" <anthony.williamsNOSPAM (AT) anthonyw (DOT) cjb.net> wrote in
message
std::locale myLocale(std::locale(),new MyNumPunctFacet);
This seems exception unsafe. What if you create the new
MyNumPunctFacet
successfully, then in std::locale() the system throws, then the new
facet is
not destroyed?
|
Correct, but you could use a little try-catch logic to fix that (I'm
going
to use A as the type as I really have no idea what you're trying to do:
try
{
A *a = new MyNumPunctFacet;
std::locale myLocale(std::locale(), a);
}
catch (...)
{
delete a;
exit(1);
}
or you could not use a pointer at all:
A a;
std::locale myLocale(std::locale(), &a);
Of course, I think the first method is better.
| Quote: |
The good news is that on Borland 6 the program works.
|
Don't keep your hopes up...programs never "always" work.
| Quote: | --
+++++++++++
Siemel Naran
|
-- MiniDisc_2k2
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Fri Jun 27, 2003 12:28 am Post subject: Re: How do I output 1000000 to 1,000,000 (oops) |
|
|
"Siemel Naran" <SiemelNaran (AT) KILL (DOT) att.net> skrev i meddelandet
news:ZCSJa.18858$0v4.1536124 (AT) bgtnsc04-news (DOT) ops.worldnet.att.net...
| Quote: | "Anthony Williams" <anthony.williamsNOSPAM (AT) anthonyw (DOT) cjb.net> wrote
in
message
std::locale myLocale(std::locale(),new MyNumPunctFacet);
This seems exception unsafe. What if you create the new
MyNumPunctFacet
successfully, then in std::locale() the system throws, then the new
facet is
not destroyed?
|
Please disregard my previous reply. There are of course two locale()
constructors involved, and the outer one *can* throw an exception.
Sorry!
Bo Persson
[email]bop2 (AT) telia (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Jun 27, 2003 4:47 pm Post subject: Re: How do I output 1000000 to 1,000,000 |
|
|
"Bo Persson" <bop2 (AT) telia (DOT) com> wrote
| Quote: | "Siemel Naran" <SiemelNaran (AT) KILL (DOT) att.net> skrev i meddelandet
news:ZCSJa.18858$0v4.1536124 (AT) bgtnsc04-news (DOT) ops.worldnet.att.net...
"Anthony Williams" <anthony.williamsNOSPAM (AT) anthonyw (DOT) cjb.net> wrote
in message
std::locale myLocale(std::locale(),new MyNumPunctFacet);
This seems exception unsafe. What if you create the new
MyNumPunctFacet successfully, then in std::locale() the system
throws, then the new facet is not destroyed?
No, because std::locale() is not allowed to throw!
|
True, but the constructor for myLocale is. And the standard is vague
whether in this case the facet will be destructed or not.
--
James Kanze GABI Software
mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter
Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45
16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Jun 27, 2003 6:07 pm Post subject: Re: How do I output 1000000 to 1,000,000 |
|
|
"Siemel Naran" <SiemelNaran (AT) KILL (DOT) att.net> wrote
| Quote: | "Anthony Williams" <anthony.williamsNOSPAM (AT) anthonyw (DOT) cjb.net> wrote in
message
std::locale myLocale(std::locale(),new MyNumPunctFacet);
This seems exception unsafe. What if you create the new
MyNumPunctFacet successfully, then in std::locale() the system throws,
then the new facet is not destroyed?
|
The standard doesn't seem to say much about this, but logically, on
calling the constructor, you have passed ownership of the facet to the
locale subsystem. At least, this is what I think the intent is, based
on §22.1.1.1.2/2. (On the other hand, the exact wording in §22.1.1.1.2
speaks of "when the last locale object containing the facet is
destroyed". In this case, since no locale object is ever constructed,
none is destroyed.)
The standard needs to be clearer. Although in practice, given that all
of the facets are managed by reference counting, I can't see any reason
why this constructor could throw.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Daniel Guest
|
Posted: Fri Jul 04, 2003 2:22 am Post subject: Re: How do I output 1000000 to 1,000,000 |
|
|
Anthony Williams wrote:
| Quote: | "Roy Gourgi" <royng (AT) videotron (DOT) ca> writes:
How could I use "cout" or "print" to print a 1000000 to 1,000,000
Imbue a locale into cout which has a numpunct facet that does the
required formatting. You might find that there is a locale on your
platform that does this already, otherwise you have to create your
own.
That *should* be as simple as the following, but it doesn't work on
all implementations, because of poor locale support.
#include <iostream
#include
#include
class MyNumPunctFacet: public std::numpunct
{
public:
explicit MyNumPunctFacet(size_t refs =
0):std::numpunct
virtual std::string do_grouping() const { return " 03"; }
|
Add:
virtual char do_thousands_sep() const { return ','; }
| Quote: | };
int main()
{
std::locale myLocale(std::locale(),new MyNumPunctFacet);
std::cout.imbue(myLocale);
std::cout<<1000000<
}
Anthony
|
With the above change, the code works on VC7.1, and probably some of the
other implementations where Anthony's implementation didn't work.
-cd
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Anthony Williams Guest
|
Posted: Tue Jul 08, 2003 11:55 pm Post subject: Re: How do I output 1000000 to 1,000,000 |
|
|
"Carl Daniel" <cpdaniel (AT) nospam (DOT) mvps.org> writes:
| Quote: | Anthony Williams wrote:
"Roy Gourgi" <royng (AT) videotron (DOT) ca> writes:
How could I use "cout" or "print" to print a 1000000 to 1,000,000
Imbue a locale into cout which has a numpunct facet that does the
required formatting. You might find that there is a locale on your
platform that does this already, otherwise you have to create your
own.
That *should* be as simple as the following, but it doesn't work on
all implementations, because of poor locale support.
#include <iostream
#include
#include
class MyNumPunctFacet: public std::numpunct
{
public:
explicit MyNumPunctFacet(size_t refs =
0):std::numpunct
virtual std::string do_grouping() const { return " 03"; }
Add:
virtual char do_thousands_sep() const { return ','; }
|
Which is what std::numpunct<char> is supposed to return anyway.
| Quote: | };
int main()
{
std::locale myLocale(std::locale(),new MyNumPunctFacet);
std::cout.imbue(myLocale);
std::cout<<1000000<
}
With the above change, the code works on VC7.1, and probably some of the
other implementations where Anthony's implementation didn't work.
|
Thanks for finding the specific bug in VC7.1.
Anthony
--
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|