* Annoying bug/warning in CXX on Digital
@ 1999-07-29 4:00 Eric Dumazet
1999-07-29 4:00 ` Daniel S. Riley
0 siblings, 1 reply; 4+ messages in thread
From: Eric Dumazet @ 1999-07-29 4:00 UTC (permalink / raw)
Hi group
Anyone knows why cxx on Digital Unix 4.0D emits this warning :
# 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)
-------------^
# cxx -V
DIGITAL C++ V6.1-029 on DIGITAL UNIX V4.0 (Rev. 878)
#
Is there a workaround (to keep the -w0 option but supress this kind of
warning)
Thanks,
Eric Dumazet
dumazet@cosmosbay.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Annoying bug/warning in CXX on Digital
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
0 siblings, 1 reply; 4+ messages in thread
From: Daniel S. Riley @ 1999-07-29 4:00 UTC (permalink / raw)
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"
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Annoying bug/warning in CXX on Digital
1999-07-29 4:00 ` Daniel S. Riley
@ 1999-07-30 4:00 ` Eric Dumazet
1999-07-30 4:00 ` Daniel S. Riley
0 siblings, 1 reply; 4+ messages in thread
From: Eric Dumazet @ 1999-07-30 4:00 UTC (permalink / raw)
To: Daniel S. Riley
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"
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Annoying bug/warning in CXX on Digital
1999-07-30 4:00 ` Eric Dumazet
@ 1999-07-30 4:00 ` Daniel S. Riley
0 siblings, 0 replies; 4+ messages in thread
From: Daniel S. Riley @ 1999-07-30 4:00 UTC (permalink / raw)
I sent a fuller response to Eric via email, but for anyone following
along at home:
Eric Dumazet <edumazet@cosmosbay.com> writes:
> egcs -Wall -Wcast-qual for example says that my exemple is correct.
> HPUX, SOLARIS, Unixware, AIX compilers are OK with this.
egcs, Sun Workshop, and any other compiler that aspires to standards
compliance will interpret
typedef void *Opaque;
const Opaque func(void);
as
void* const func(void);
because that is what standard C semantics require.
> 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 difference is that cxx is (IMHO) doing a much better job--it at
least tells you that const in that position doesn't do a darn thing.
The other compilers interpret it the same way, they just don't tell
you that it doesn't do anything.
--
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"
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~1999-07-30 4:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
1999-07-30 4:00 ` Daniel S. Riley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox