 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Paolo Carlini Guest
|
Posted: Wed May 05, 2004 5:05 pm Post subject: Traits::compare vs signed char type vs memcmp |
|
|
Hi,
in the GNU libstdc++-v3 project we have this PR:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15276
which basically points out that, on machines characterized by a signed
char type, implementing traits<char>::compare in terms of memcmp is
incorrect on the face of Table 37.
The letter of the standard appear to support this, but, on the other
hand, all the implementations that I have checked, actually rely on
memcmp (and wmemcmp) for the specializations.
What do you think?
Thanks,
Paolo.
---
[ 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 |
|
 |
Alberto Barbati Guest
|
Posted: Thu May 06, 2004 4:30 am Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
Paolo Carlini wrote:
| Quote: | Hi,
in the GNU libstdc++-v3 project we have this PR:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15276
which basically points out that, on machines characterized by a signed
char type, implementing traits<char>::compare in terms of memcmp is
incorrect on the face of Table 37.
The letter of the standard appear to support this, but, on the other
hand, all the implementations that I have checked, actually rely on
memcmp (and wmemcmp) for the specializations.
What do you think?
|
According to my intepretation of table 37 alone, it would be perfectly
reasonable to define char_traits<char>::lt like this:
bool char_traits<char>::lt(char c, char d)
{
return static_cast<unsigned char>(c) < static_cast
}
With this definition, implementing char_traits<char>::compare in terms
of memcmp would be perfectly consistent.
However, 21.1.3.1/6 explicitly states that lt() must be identical to the
built-in operator "<" (without casts) :-(
With this constraint, it's clearly impossible to implement compare() in
terms of memcmp() without violating the semantic of table 37, if char is
signed. In order to avoid the explicit loop, a conforming implementation
should use a different function, hopefully resolving to a compiler
intrinsic if available.
OTOH, a DR could be issued with one of two possible solutions:
1) relax the semantic of compare() (at least for the char and wchar_t
specialization)
2) remove the restriction on lt() for char and wchar_t specialization
Alberto
---
[ 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 |
|
 |
Paolo Carlini Guest
|
Posted: Thu May 06, 2004 5:02 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
Alberto Barbati wrote:
| Quote: | However, 21.1.3.1/6 explicitly states that lt() must be identical to the
built-in operator "<" (without casts)
|
Exactly, :-(
| Quote: | OTOH, a DR could be issued with one of two possible solutions:
1) relax the semantic of compare() (at least for the char and wchar_t
specialization)
2) remove the restriction on lt() for char and wchar_t specialization
|
In my personal opinion a DR is in order: personally, I find unreasonable
being forced by the C++ standard to compare /the very same/ char type as
signed whereas all the C standard functions compare it as unsigned.
Paolo.
---
[ 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 |
|
 |
James Kuyper Guest
|
Posted: Thu May 06, 2004 5:06 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
[email]AlbertoBarbati (AT) libero (DOT) it[/email] (Alberto Barbati) wrote in message news:<u3gmc.170690$Kc3.5429610 (AT) twister2 (DOT) libero.it>...
...
| Quote: | According to my intepretation of table 37 alone, it would be perfectly
reasonable to define char_traits<char>::lt like this:
bool char_traits<char>::lt(char c, char d)
{
return static_cast<unsigned char>(c) < static_cast
}
|
With that definition, if c is negative and d is not, then
std::char_traits<char>::lt(c,d) return false. How would you argue that
this counts as a conforming implementation of the specification that
it "yields: whether c is to be treated as less than d"?
---
[ 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 |
|
 |
Alberto Barbati Guest
|
Posted: Fri May 07, 2004 12:42 am Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
James Kuyper wrote:
| Quote: | AlbertoBarbati (AT) libero (DOT) it (Alberto Barbati) wrote in message news:<u3gmc.170690$Kc3.5429610 (AT) twister2 (DOT) libero.it>...
According to my intepretation of table 37 alone, it would be perfectly
reasonable to define char_traits<char>::lt like this:
bool char_traits<char>::lt(char c, char d)
{
return static_cast<unsigned char>(c) < static_cast
}
With that definition, if c is negative and d is not, then
std::char_traits<char>::lt(c,d) return false. How would you argue that
this counts as a conforming implementation of the specification that
it "yields: whether c is to be treated as less than d"?
|
The fact is that the numeric values of the characters have nothing to do
with the notion of "order". For example, it is perfectly reasonable to
consider character U+A0 NO-BREAK SPACE "greater" than U+41 LATIN CAPITAL
LETTER A, despite the fact that the first is stored with negative value
and the second with a positive one (assuming char is 8-bit and signed).
That is also consistent with the C library, as, for example,
strcmp("xa0", "x41") returns 1.
Alberto
---
[ 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 |
|
 |
llewelly Guest
|
Posted: Fri May 07, 2004 4:24 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
[email]pcarlini (AT) suse (DOT) de[/email] (Paolo Carlini) writes:
| Quote: | Alberto Barbati wrote:
However, 21.1.3.1/6 explicitly states that lt() must be identical to
the built-in operator "<" (without casts) :-(
Exactly, :-(
OTOH, a DR could be issued with one of two possible solutions:
1) relax the semantic of compare() (at least for the char and
wchar_t specialization)
2) remove the restriction on lt() for char and wchar_t specialization
In my personal opinion a DR is in order: personally, I find unreasonable
being forced by the C++ standard to compare /the very same/ char type as
signed whereas all the C standard functions compare it as unsigned.
[snip] |
But from a user perspective it is unreasonable that the standard
allow lt() and compare() to be inconsistent.
---
[ 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 |
|
 |
James W. McKelvey Guest
|
Posted: Fri May 07, 2004 4:24 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
Paolo Carlini wrote:
| Quote: |
Alberto Barbati wrote:
However, 21.1.3.1/6 explicitly states that lt() must be identical to the
built-in operator "<" (without casts) :-(
Exactly, :-(
OTOH, a DR could be issued with one of two possible solutions:
1) relax the semantic of compare() (at least for the char and wchar_t
specialization)
2) remove the restriction on lt() for char and wchar_t specialization
In my personal opinion a DR is in order: personally, I find unreasonable
being forced by the C++ standard to compare /the very same/ char type as
signed whereas all the C standard functions compare it as unsigned.
|
Why then allow char to be signed at all, if the signs are to be ignored?
The C standard functions are irrelevant here anyway.
--
What portion in the world can the artist have
Who has awakened from the common dream
But dissipation and despair?
-- William Butler Yeats
---
[ 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 |
|
 |
James Kuyper Guest
|
Posted: Fri May 07, 2004 4:52 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
[email]AlbertoBarbati (AT) libero (DOT) it[/email] (Alberto Barbati) wrote in message news:<dXzmc.41690$Qc.1633658 (AT) twister1 (DOT) libero.it>...
| Quote: | James Kuyper wrote:
[email]AlbertoBarbati (AT) libero (DOT) it[/email] (Alberto Barbati) wrote in message news:<u3gmc.170690$Kc3.5429610 (AT) twister2 (DOT) libero.it>...
According to my intepretation of table 37 alone, it would be perfectly
reasonable to define char_traits<char>::lt like this:
bool char_traits<char>::lt(char c, char d)
{
return static_cast<unsigned char>(c) < static_cast
}
With that definition, if c is negative and d is not, then
std::char_traits<char>::lt(c,d) return false. How would you argue that
this counts as a conforming implementation of the specification that
it "yields: whether c is to be treated as less than d"?
The fact is that the numeric values of the characters have nothing to do
with the notion of "order". For example, it is perfectly reasonable to
consider character U+A0 NO-BREAK SPACE "greater" than U+41 LATIN CAPITAL
LETTER A, despite the fact that the first is stored with negative value
and the second with a positive one (assuming char is 8-bit and signed).
|
What you're talking about is a locale-specific feature that is covered
by the collate locale category. That's not what the 'lt' member of
char_traits is for.
---
[ 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 |
|
 |
Paolo Carlini Guest
|
Posted: Fri May 07, 2004 4:53 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
llewelly wrote:
| Quote: | But from a user perspective it is unreasonable that the standard
allow lt() and compare() to be inconsistent.
|
Quite to the contrary, the goal is consistency between lt() and
compare(): it could be easily achieved in many ways, for instance
as sketched by Alberto.
Paolo.
---
[ 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 |
|
 |
Paolo Carlini Guest
|
Posted: Fri May 07, 2004 4:53 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
James W. McKelvey wrote:
| Quote: | Why then allow char to be signed at all, if the signs are to be ignored?
|
The same could be asked in the case of the C standard, however.
| Quote: | The C standard functions are irrelevant here anyway.
|
Well, do you really think so? Do you like comparing two char arrays
via memcmp then passing the arrays to a basic_string constructor and
"voila'" starting comparing /the very same/ data in a different way?
Paolo.
---
[ 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 |
|
 |
Paolo Carlini Guest
|
Posted: Fri May 07, 2004 6:06 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
llewelly wrote:
| Quote: | But from a user perspective it is unreasonable that the standard
allow lt() and compare() to be inconsistent.
|
Quite to the contrary, the goal is consistency between lt() and
compare(): it could be easily achieved in many ways, for instance
as sketched by Alberto.
Paolo.
---
[ 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 |
|
 |
llewelly Guest
|
Posted: Fri May 07, 2004 9:06 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
[email]pcarlini (AT) suse (DOT) de[/email] (Paolo Carlini) writes:
| Quote: | llewelly wrote:
But from a user perspective it is unreasonable that the standard
allow lt() and compare() to be inconsistent.
Quite to the contrary,
|
Not contrary at all.
| Quote: | the goal is consistency between lt() and
compare():
|
This is what I want to hear, thank you.
| Quote: | it could be easily achieved in many ways, for instance
as sketched by Alberto.
|
Alberto made two suggestions. Only the second seems to guarantee
consistency between lt() and compare().
---
[ 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 |
|
 |
Paolo Carlini Guest
|
Posted: Fri May 07, 2004 9:37 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
llewelly wrote:
| Quote: | Alberto made two suggestions. Only the second seems to guarantee
consistency between lt() and compare().
|
Indeed, you are right. Actually, the plain char case is the one
really troublesome, since, for wchar_t, C99, 7.24.4.4,p1 holds.
Paolo.
---
[ 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 |
|
 |
James W. McKelvey Guest
|
Posted: Sun May 09, 2004 3:29 am Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
Paolo Carlini wrote:
| Quote: |
James W. McKelvey wrote:
Why then allow char to be signed at all, if the signs are to be ignored?
The same could be asked in the case of the C standard, however.
The C standard functions are irrelevant here anyway.
Well, do you really think so? Do you like comparing two char arrays
via memcmp then passing the arrays to a basic_string constructor and
"voila'" starting comparing /the very same/ data in a different way?
|
Suppose I use memcmp to compare SIGNED char arrays; the same arrays
passed to a basic_string constructor will compare the very same data in
a different way. Are you saying that you want
all char types to compare as if unsigned?
But memcmp doesn't work with chars anyway. It works with unsigned bytes;
so if I had to use it, I would be very careful and would know the
limitations.
std::string and char_traits are not C, but C++, and can do whatever they
are defined to do. If there is a requirement that they follow C in some
manner, I don't see it in the standard.
memcmp and strcmp are C functions that do something similar to C++
functionality; they are not exact replacements, and must be used with
care.
This is the way I approach it: C++ is trying to be better than C; it
can't do that if everything has to be done the way C does things.
Jim
--
What portion in the world can the artist have
Who has awakened from the common dream
But dissipation and despair?
-- William Butler Yeats
---
[ 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 |
|
 |
Paolo Carlini Guest
|
Posted: Sun May 09, 2004 6:43 pm Post subject: Re: Traits::compare vs signed char type vs memcmp |
|
|
Hi,
James W. McKelvey wrote:
| Quote: | But memcmp doesn't work with chars anyway. It works with unsigned bytes;
so if I had to use it, I would be very careful and would know the
limitations.
|
This one *could* be a very good point ;)
Unfortunately, the C Standard *always* talks about *characters* in
7.21.4...
| Quote: | std::string and char_traits are not C, but C++, and can do whatever they
are defined to do. If there is a requirement that they follow C in some
manner, I don't see it in the standard.
|
Interestingly for the historian the 1995 draft had the requirement
that memcmp be used in the implementation of compare.
| Quote: | memcmp and strcmp are C functions that do something similar to C++
functionality; they are not exact replacements, and must be used with
care.
This is the way I approach it: C++ is trying to be better than C; it
can't do that if everything has to be done the way C does things.
|
This is all very reasonable. My personal point of view is very similar.
However, remember that C++ was born to be as compatible as possible
with C and that there is an ongoing effort to *improve* that.
Also, the established behavior in this area is very strong and
pervasive. Not considering the performance side of the issue...
Paolo.
---
[ 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
|
|