c++/7562: strange behaviour with typedef and consts
Nicolas Noble
nicolas@nobis-crew.org
Fri Aug 9 14:06:00 GMT 2002
>Number: 7562
>Category: c++
>Synopsis: const with typedefs doesn't work as expected.
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: unassigned
>State: open
>Class: rejects-legal
>Submitter-Id: net
>Arrival-Date: Fri Aug 09 13:46:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:
>Release: 3.1
>Organization:
>Environment:
System: Linux the-babel-tower 2.4.18 #2 Sun Apr 28 23:46:35 CEST 2002 i686 unknown
Architecture: i686
host: i386-slackware-linux-gnu
build: i386-slackware-linux-gnu
target: i386-slackware-linux-gnu
configured with: ../gcc-3.1/configure --prefix=/usr --enable-shared --with-gnu-ld --enable-threads --verbose --target=i386-slackware-linux --host=i386-slackware-linux
>Description:
Here is a problem I saw first with the zlib header. It's very easy to understand, I think you'll be able to understand
with the How-To-Repeat section. In brief, it's when having a "const" with a previously typedef'ed type, the compiler
forgets the "const", which only produces a warning in C compilation mode, but produces an error in C++ compilation mode.
>How-To-Repeat:
Here is the simple source code:
$ cat typedef.cc
typedef void * voidp;
void func1(const voidp p) { }
void func2(const void * p) {
func1(p);
}
and what happen:
$ gcc-3.1 -c typedef.cc
typedef.cc: In function `void func2(const void*)':
typedef.cc:6: invalid conversion from `const void*' to `void*'
which is the worse case I think. With a .c extension it only does that:
$ gcc-3.1 -c typedef.c
typedef.c: In function `func2':
typedef.c:6: warning: passing arg 1 of `func1' discards qualifiers from pointer target type
>Fix:
Actually the only "fix" I was able to think of was to forget typedef'ing the voidp, but this is a pain with the zlib.h
since it has "voidp" everywhere. Another solution, but very ugly, is to #define voidp void * for example.
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the Gcc-prs
mailing list