Details

[Home]

Issue of the Implementation # S0770

Brief

Behaviour of basic_string::compare may not comply with ISO/IEC 14882 on IA64 systems

Detailed Description

The description of basic_string::compare(size_t, size_t, basic_string const&) states the following (see section 21.3.6.8):

int compare(size_type pos1, size_type n1, const basic_string& str) const;
Returns: basic_string(*this,pos1,n1).compare(str).

As it is also stated in the same section of the standard,
    int compare(const basic_string& str) const
uses char_traits::compare() function to compare char*-sequences. The latter function, in turn, calls __builtin_memcmp() for this purpose.

However, on IA64 systems, the return value of __builtin_memcmp() depends not only on the values of its arguments.

This results in the situations when, in some cases (see the example below), compare(pos1, n1, str) returns 32, while basic_string(*this,pos1,n1).compare(str) returns 1. The return values have the same sign but still they are different. That is why, formally, the requirement for compare functions stated in section 21.3.6.8 of ISO/IEC 14882 is violated.

Problem location(s) in the standard

Linux Standard Base C++ Specification 3.2, Chapter 9. Libraries, 9.1. Interfaces for libstdcxx that refers ISO/IEC 14882: 2003 Programming languages --C++, section 21.3.6.8

Example

#include <string>
#include <cstdio>

#define CHAR_T     char
#define STR_LEFT   "The quick brown foxy"
#define STR_RIGHT  "e quick brown Fox"
#define POS_VALUE  2
#define N_VALUE    str_src.size() - 3

using namespace std;

int
main(int argc, char* argv[])
{
    basic_string<CHAR_T> str_src(STR_LEFT);
    basic_string<CHAR_T> str(STR_RIGHT);
    
    size_t pos = POS_VALUE;
    size_t n = N_VALUE;

    basic_string<CHAR_T>    str1(str_src, pos, n);
    
    int ret_mustbe = str1.compare(str);
    int ret = str_src.compare(pos, n, str);

    printf("The function should have returned %d, it returned %d\n", 
        ret_mustbe, ret);
    
    return 0;
}

Component

libstdc++

Environment

Architectures

IA64

[Home]