From: Eric Dumazet <edumazet@cosmosbay.com>
To: "Daniel S. Riley" <dsr@mail.lns.cornell.edu>
Subject: Re: Annoying bug/warning in CXX on Digital
Date: 1999/07/30 [thread overview]
Message-ID: <37A1D9A6.FD5138C5@cosmosbay.com> (raw)
In-Reply-To: shso67722i.fsf@lnscu4.lns.cornell.edu
Thanks for your usefull explanations.
The problem is that this kind of code is correct :
void *memcpy(void *s1, const void *s2, size_t n) ;
So "const void *" is different of "void *"
a function returning "void *" does say that you can do :
char *ptr = memcpy(zone, "hello", 6) ;
ptr[3] = 'T' ;
but you cannot do :
const void *search_in_a_const_zone_first_null(const void *zone) ;
char *ptr = search_in_a_const_zone_first_null(zone) ;
ptr[1] = 'T' ;
egcs -Wall -Wcast-qual for example says that my exemple is correct.
HPUX, SOLARIS, Unixware, AIX compilers are OK with this.
Now I realise that this warning says 'your const attribute is ignored'.
So if I want the compiler checks that const invariance is preserved, I
bet that Digital CXX doesn't do the job. It's quite a pity.
The solution to hide these warnings is not an option for me.
typedef void * Opaque ;
typedef const void *const_Opaque ;
Opaque MyMemcpy(Opaque, const_Opaque, size_t) ;
The solution of having to declare 2 typedefs for each type (with/without
the const qualifier) is quite ugly dont you think ?
Eric Dumazet
"Daniel S. Riley" wrote:
>
> Eric Dumazet <Eric.Dumazet@cosmosbay.com> writes:
> > # cat bug.C
> > typedef void * Opaque ;
> >
> > const Opaque func(void)
> > {
> > return 0 ;
> > }
> > # cxx -c -w0 bug.C
> > cxx: Info: bug.C, line 3: type qualifier on return type is meaningless
> > const Opaque func(void)
> > -------------^
>
> As I understand this, the type of "const Opaque" is "void* const"
> (const pointer to void), not "const void*" (pointer to const void).
> Since "pointer to void" as a return value isn't modifiable, declaring
> it const is a no-op--the "const" does nothing, similar to "const int
> func()". If the type were "const void*", you wouldn't get the
> warning, as the "void" pointed to by the "const void*" is potentially
> modifiable, and hence declaring it const means something.
>
> If that reads like nonsense (and it probably will the first few times),
> you need to meditate on the difference between "const pointer to void"
> and "pointer to const void". Also try guessing which of these produces
> cxx warnings or errors:
>
> typedef void* Opaque;
>
> const Opaque f1() { return 0; }
> const void* f2() { return 0; }
> void* const f3() { return 0; }
> const Opaque& f4() { static Opaque i = 0; return i; }
>
> main()
> {
> Opaque p1 = f1();
> Opaque p2 = f2();
> Opaque p3 = f3();
> Opaque& p4 = f4();
> }
>
> > Is there a workaround (to keep the -w0 option but supress this kind of
> > warning)
>
> First, consider whether cxx might be right. The "const" isn't doing
> anything--if you thought it was, then you probably want
>
> typedef const void * Opaque ;
>
> Opaque func(void)
> {
> return 0 ;
> }
>
> instead. If you really just want to suppress the warning, here's the
> recipe:
>
> dsr_lnscu4% cxx -c -w0 -msg_display_tag warn.cc
> cxx: Warning: warn.cc, line 3: type qualifier on return type is meaningless (D:useless_type_qualifier_on_return_type)
> const Opaque func(void)
> -------------^
> dsr_lnscu4% cxx -c -w0 -msg_disable useless_type_qualifier_on_return_type warn.cc
> dsr_lnscu4%
>
> or
>
> dsr_lnscu4% cxx -c -w0 -msg_display_number warn.cc
> cxx: Warning: warn.cc, line 3: #824-D type qualifier on return type is meaningless
> const Opaque func(void)
> -------------^
> dsr_lnscu4% cxx -c -w0 -msg_disable 824 warn.cc
> dsr_lnscu4%
>
> --
> Dan Riley dsr@mail.lns.cornell.edu
> Wilson Lab, Cornell University <URL:http://www.lns.cornell.edu/~dsr/>
> "History teaches us that days like this are best spent in bed"
next prev parent reply other threads:[~1999-07-30 4:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-07-29 4:00 Annoying bug/warning in CXX on Digital Eric Dumazet
1999-07-29 4:00 ` Daniel S. Riley
1999-07-30 4:00 ` Eric Dumazet [this message]
1999-07-30 4:00 ` Daniel S. Riley
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox