Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case
From: John Smith (johnsmith_smith_at_caramail.com)
Date: 08/03/04
- Next message: Igor Tandetnik: "Re: Questions about UTF-8 std::strings and wstrings"
- Previous message: Olav: "Re: Questions about UTF-8 std::strings and wstrings"
- In reply to: Doug Harrison [MVP]: "Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case"
- Next in thread: Doug Harrison [MVP]: "Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case"
- Reply: Doug Harrison [MVP]: "Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case"
- Messages sorted by: [ date ] [ thread ]
Date: 3 Aug 2004 13:51:35 -0700
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message news:<l66ug0h4aqv5are9fjgr897j0qbmcc5qfb@4ax.com>...
> John Smith wrote:
>
> >Hi,
> >
> >I would like to understand what's going on here:
> >
> >const void *foo[10];
> >
> >void bar(void)
> >{
> > // issues warning C4090: 'function' : different 'const' qualifiers
> > memset(foo, 0, sizeof(const void *) * 10);
> >}
> >
> >Why does the VC++ compiler (7.1.3088) issue a warning here? The foo
> >array is not declared constant. Only the cell of the array is of type
> >const void *.
> >This is my first question.
> >
> >Now, if I replace the code with:
> >
> >typedef void *MYTYPE;
> >typedef const MYTYPE MYCONSTTYPE;
> >
> >MYCONSTTYPE foo[10];
> >
> >memset(foo, 0, sizeof(const void *) * 10);
> >
> >then no warning is issued, although the code looks quite identical.
> >
> >How is this possible?
>
> Don't you have that backwards? The first one is legal, but the second one is
> not. The second actually declares foo to be an array of void* const. You're
> probably thinking that:
>
> typedef const MYTYPE MYCONSTTYPE;
>
> makes MYCONSTTYPE equivalent to:
>
> const void*
>
> as if simple textual substitution were performed. But typedefs don't work
> that way. In fact, given that MYTYPE is a typedef, it doesn't matter where
> you place the const; the following are equivalent:
>
> typedef const MYTYPE MYCONSTTYPE;
> typedef MYTYPE const MYCONSTTYPE;
>
> Given that MYTYPE is void*, they both mean:
>
> void* const
>
> You can think of it this way. The const and volatile modifiers don't
> penetrate into typedefs. Instead, they modify the thing being declared,
> which above is MYCONSTTYPE.
>
> P.S. You can replace:
>
> memset(foo, 0, sizeof(const void *) * 10);
>
> with:
>
> memset(foo, 0, sizeof(void *) * 10);
>
> or better still, given that foo is an array:
>
> memset(foo, 0, sizeof(foo));
>
> Speaking ultra-pedantically, memsetting pointers to zero isn't guaranteed to
> set them to null, though I can't name a system where it doesn't.
Thanks for your reply Doug.
Do you have an answer to my first question?
- Next message: Igor Tandetnik: "Re: Questions about UTF-8 std::strings and wstrings"
- Previous message: Olav: "Re: Questions about UTF-8 std::strings and wstrings"
- In reply to: Doug Harrison [MVP]: "Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case"
- Next in thread: Doug Harrison [MVP]: "Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case"
- Reply: Doug Harrison [MVP]: "Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|