Bug 49020 - Invalid std::strchr prototype in cstring
Summary: Invalid std::strchr prototype in cstring
Status: CLOSED DUPLICATE of bug 33935
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-17 11:37 UTC by __vic
Modified: 2011-05-18 10:35 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description __vic 2011-05-17 11:37:37 UTC
Shall be

const char *strchr(const char * , int );
char *strchr(char * , int );

according to Standard.
But we have

char *strchr(char * , int );

and invalid

char *strchr(const char * , int );

from string.h
Comment 1 Jonathan Wakely 2011-05-17 12:17:24 UTC
You haven't said which version of gcc or which platform.

With a recent GCC and recent glibc I get:

extern "C++"
{
extern char *strchr (char *__s, int __c)
     throw () __asm ("strchr") __attribute__ ((__pure__)) __attribute__ ((__nonn
ull__ (1)));
extern __const char *strchr (__const char *__s, int __c)
     throw () __asm ("strchr") __attribute__ ((__pure__)) __attribute__ ((__nonn
ull__ (1)));
}
Comment 2 Jonathan Wakely 2011-05-17 12:22:25 UTC
dup of PR 33935 - also related to PR 30928

*** This bug has been marked as a duplicate of bug 33935 ***
Comment 3 __vic 2011-05-17 12:24:56 UTC
(In reply to comment #1)
> You haven't said which version of gcc or which platform.
> 
Actually 3.4.3 on Linux and Windows (MinGW) but in SVN trunk version (r169421)
/trunk/libstdc++-v3/include/c_std/cstring
I have not found any changes.
Comment 4 __vic 2011-05-17 12:31:50 UTC
(In reply to comment #1)
> With a recent GCC and recent glibc I get:
> 
In which version of GCC can I find valid implementation?
Comment 5 __vic 2011-05-17 12:41:29 UTC
GCC 4.1.1 - still no luck
Comment 6 Jakub Jelinek 2011-05-17 13:07:24 UTC
In glibc 2.10 and later together with gcc 4.4 and later.
Comment 7 __vic 2011-05-17 13:25:31 UTC
Ok, I'll try to upgrade my GCC version.
Thanx
Comment 8 Jonathan Wakely 2011-05-17 13:38:57 UTC
3.4 and 4.1 are ancient history, active release series are listed on the home page, http://gcc.gnu.org/
Comment 9 __vic 2011-05-18 05:33:24 UTC
The libstdc++ uses glibc anyway? What about alternative implementations like hp-gcc for HP-UX?
Comment 10 __vic 2011-05-18 05:36:38 UTC
(In reply to comment #8)
> 3.4 and 4.1 are ancient history, active release series are listed on the home
> page, http://gcc.gnu.org/

Yes, I know. But I have no ability to upgrade them in all installations
Comment 11 Jonathan Wakely 2011-05-18 08:34:50 UTC
(In reply to comment #9)
> The libstdc++ uses glibc anyway? What about alternative implementations like
> hp-gcc for HP-UX?

I don't know what hp-gcc is, but on non-glibc platforms the overloads aren't correct, see PR 33935


(In reply to comment #10)
> (In reply to comment #8)
> > 3.4 and 4.1 are ancient history, active release series are listed on the home
> > page, http://gcc.gnu.org/
> 
> Yes, I know. But I have no ability to upgrade them in all installations

I didn't say you should upgrade them, but there's no point reporting bugs against old, unmaintained versions.  You could install a current release to test before reporting a bug though.
Comment 12 __vic 2011-05-18 09:06:26 UTC
(In reply to comment #11)
> I don't know what hp-gcc is, but on non-glibc platforms the overloads aren't
> correct, see PR 33935
> 
PR 33935 is mostly about overloads in string.h. I'm not interested in them.

Could you clarify what happen in following case. C-implementation don't provides overloads, only standard C prototype in string.h. Can I get standard-conformant prototypes by including cstring from libstdc++ on such platform?
According to the trunk version of cstring answer is "No". Is this true?
Comment 13 Paolo Carlini 2011-05-18 09:26:14 UTC
If you don't use glibc 2.10+ as C library the answer is No indeed.
Comment 14 Jonathan Wakely 2011-05-18 09:28:41 UTC
As stated at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33935#c4
Comment 15 __vic 2011-05-18 09:30:59 UTC
The following code would work anyway:

namespace std {

#ifdef __CORRECT_ISO_CPP_STRING_H_PROTO

using ::strchr;

#else

inline const char *strchr(const char *__s, int __c)
{
    return ::strchr(__s, __c);
}
inline char *strchr(char *__s, int __c)
{
    return ::strchr(__s, __c);
}

#endif

}
Comment 16 __vic 2011-05-18 09:55:34 UTC
Initial problem is that the following standard-conforming code is not compiled by GCC.

#include<cstring>

template<class Iter>
void f(Iter i1, Iter i2)
{
}

int main()
{
    const char *st = "abc";
    f(st, std::strchr(st, '\0'));
}

Yes, workaround is obvious: add the const_cast to the return value. But is there a way to do without it?
Comment 17 Paolo Carlini 2011-05-18 10:08:22 UTC
.

*** This bug has been marked as a duplicate of bug 33935 ***
Comment 18 Jonathan Wakely 2011-05-18 10:22:42 UTC
(In reply to comment #15)
> The following code would work anyway:

No, it would make this ambiguous:

#include <cstring>
using namespace std;

int main()
{
    strchr("foo", 'f');
}


(In reply to comment #16)
> Yes, workaround is obvious: add the const_cast to the return value. But is
> there a way to do without it?

Only with assistance from the C library, which is why this is a dup of PR 33935
Comment 19 __vic 2011-05-18 10:35:51 UTC
(In reply to comment #18)
> (In reply to comment #15)
> > The following code would work anyway:
> 
> No, it would make this ambiguous:
> 
> #include <cstring>
> using namespace std;
> 
> int main()
> {
>     strchr("foo", 'f');
> }
> 
> 
> (In reply to comment #16)
> > Yes, workaround is obvious: add the const_cast to the return value. But is
> > there a way to do without it?
> 
> Only with assistance from the C library, which is why this is a dup of PR 33935

Yeah. Seems like it's not possible to do if C-library implementer didn't care about this...