question about strstr() in g++

Peter van Hoof p.vanhoof@oma.be
Mon Aug 24 20:31:00 GMT 2009


Marc Glisse wrote:
> On Sun, 23 Aug 2009, Peter van Hoof wrote:
> 
>> Nikos Chantziaras wrote:
>>> On 08/23/2009 09:32 PM, Peter van Hoof wrote:
>>>> Hi,
>>>>
>>>> My understanding is that in C++ two overloaded versions of strstr()
>>>> should be defined:
>>>>
>>>> const char * strstr ( const char * str1, const char * str2 );
>>>> char * strstr ( char * str1, const char * str2 );
>>>>
>>>> (http://www.cplusplus.com/reference/clibrary/cstring/strstr/ but also
>>>> Stroustrup's book section 20.4.1 (special edition))
>>>>
>>>> The way I read this, the following C++ code should be invalid:
>>>>
>>>> #include <cstring>
>>>> char *backdoor(const char *a, const char *b)
>>>> {
>>>> return std::strstr(a, b);
>>>> }
>>>>
>>>> since it is trying to remove the const-ness of the std:strstr() result.
>>>> However, g++ accepts this. It doesn't even give a warning with g++ 
>>>> -ansi
>>>> -Wall -W -pedantic. Most compilers I tried accept this, but it is my
>>>> understanding that VS08 rejects this code.
>>>>
>>>> So am I reading this wrong, or is there a bug in g++?
>>>
>>> GCC 4.4.1:
>>>
>>>   In function 'char* backdoor(const char*, const char*)':
>>>   error: invalid conversion from 'const char*' to 'char*'
>>>
>>> So it seems you're using an old GCC which has this bug.
>>
>> Actually, I tried g++ 4.5.0, 4.4.1, 4.3.4, 4.2.4, 4.1.2, 4.0.4, and
>> 3.3.6. They all accept the code with the command line switches I posted.
>> What command line switches did you use?
>>
>> Peter.
> 
> The problem is with libc more than g++. Solaris libc has been doing the 
> right thing for years, but g++ doesn't take advantage of it. GNU libc 
> quite recently moved to the right prototypes (for some headers) and 
> g++>=4.4 takes advantage of it. You can find many discussions of this in 
> bugzilla.

I see. I not only need a fairly new g++, but also a newer Linux 
installation... Well, at least the problem is clearer now...

Thanks!

Peter.



More information about the Gcc-help mailing list