question about strstr() in g++
Peter van Hoof
p.vanhoof@oma.be
Mon Aug 24 13:50:00 GMT 2009
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.
More information about the Gcc-help
mailing list