 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Martin Sebor Guest
|
Posted: Fri Apr 02, 2004 11:16 pm Post subject: Re: A comment about library issue 434 |
|
|
...
| Quote: | template <class charT, class traits
basic_string
to_string (charT = '0', charT = '1') const;
That's horrible, sorry First of all, those default args only works
for charT=char.
|
It works for both char and wchar_t, the two types that the template is
most commonly going to be instantiated on. Programs that use character
types other than those two are, IMO, exceedingly rare, and can
specialize to_string on their own value of charT (although doing so
would be tedious for a generic bitset).
| Quote: |
...
As you propose it, you make
b.to_string
ill-working (because it compiles, but...),
|
How so? It compiles and works just fine. Why is it ill-working (and
what do you mean by it)? (Note that the expression ('0' == L'0' && '1'
== L'1') must and does hold for all known locales.)
| Quote: | and
b.to_string<MyCharType, ...>();
ill-formed, or anyway illegal (note that MyCharType must be a POD
type)
|
Yes. Making this corner case well-formed requires IMO needlessly
complex machinery.
| Quote: |
That said, take a look at the implementation I give in reply to Carl
(or the analogous one at http://tinyurl.com/2rvvf - I'm going to fight
on boost to deprecate that! ). The only way to get generic CharT
versions of '0' and '1' is to use ctype's widen (see also lib issue
303), but of what locale? In to_string, you don't have a locale
parameter, so you have no other choice than using the global one. But
why I have to #include <locale> and cope with the ctype facet if the
function is so constrained? With streams I can imbue whatever I want.
With to_string I can only use the global locale.
|
I don't think you want to drag in all of locale just to format a
string of zeros ones. The proposed change is a tradeoff between
simplicity and robustness. The handful of programs that want to go
through locale can easily do that by bypassing bitset::to_string() and
implementing their own formatting.
Incidentally, as I mentioned above, ctype<wchar_t>::widen('0') is for
all intents and purposes required to be equal to L'0' (i.e., the same
as (wchar_t)'0' so all this complexity won't really buy you anything
but slow performance in the common case. And since ctype<charT> is not
required to be provided for any charT other than char and wchar_t,
your code won't compile either (unless the user writes their own
specialization of ctype on their charT; that's a lot more work than
specializing to_string on their own charT).
| Quote: |
...
However it complicates a bit the stream operators, as they have to
check for the existence of the bitset_digits<CharT> facet in the
stream locale. It's also a bit less convenient to use:
|
Right. Too much complexity for something so simple.
| Quote: |
...
The third solution is to use a special class with its own operators
and >>. That is, you define special formatting in terms of a special
class:
bit_alpha<char> format ('*', 'x', my_bitset);
std::cout << format;
Such a class could be provided as an extension. In that case the
stream operators in std::bitset would simply construct a suitable
bit_alpha object and delegate the actual work to it. The annoyance to
specify the character type (as done above) can be eliminated too.
|
I'm not sure I quite follow this but again, it seems a lot more
invasive (in terms of changes to the standard text) than the
admittedly limited extension I propose. I think Dietmar Kuhl said he'd
try to propose something along these lines but I haven't seen it yet.
Martin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| 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
|
|