This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

G++ library bug


Hi,

I think there are some bug(s) in the string standard C++ classes.


Compiler: gcc version egcs-2.91.57 19980901 (egcs-1.1 release)

The string::compare prototypes don't match Stroustrup's 3rd ed.

He has:

    int string::compare(size_type pos, size_type n, const basic_string &s)
const;

You have:

    int compare (const basic_string& str, size_type pos = 0, size_type
	n = npos) const;

Now frankly, yours makes much more sense because it allows defaults, but
it's confusing when the book says something else.  Maybe provide both
at least?

Additionally, Stroustrup says "strings can be compared to strings of
their own type and to arrays of characters with the same character
type."  But that doesn't seem to work.

Sample program:

----------------------------->*snip*<--------------------------------
#include <string>

int
main(void)
{
    int     haha;
    string  hoho = "BEAVIS";
    string  hehe = "BEAVIS AND BUTTHEAD";

    haha = hoho.compare("BEAVIS AND BUTTHEAD", 0, 5);
    cerr << "first haha should be 0, is " << haha << '\n';

    haha = hehe.compare("BEAVIS", 0, 5);
    cerr << "second haha should be 0, is " << haha << '\n';

    return 0;
}
----------------------------->*snip*<--------------------------------

The output I get is:

.../tests> ./bug
first haha should be 0, is 1
second haha should be 0, is 14

As a workaround, I'm using == and substr, like:

	if (hehe.substr(0,6) == "BEAVIS") {
		....

regards,
Matt

-- 
Matt Bonner                     |  mailto:mattb@bpo.hp.com
Hewlett-Packard Company         |  Snail: Avenida Graells, 501
Ink-jet Supplies Business Unit  |         08190 Sant Cugat del Valles
"Simplify!" -Thoreau            |         Barcelona, Spain


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]