Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: John Smith (johnsmith_smith_at_caramail.com)
Date: 08/03/04


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?



Relevant Pages

  • Re: reverse_iterator
    ... you might want to define an iterator class ... a fixed-size C array. ... typedef T value_type; ... typedef const T& const_reference; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: VC++.NET 2003 should not issue C4090 ompiler warning in the following case
    ... Only the cell of the array is of type ... >const void *. ... >typedef const MYTYPE MYCONSTTYPE; ...
    (microsoft.public.vc.language)
  • Re: Pointers to functions
    ... Prefix it with typedef and place parentheses round the ... typedef void (int, char const *); ... However you cannot have an array of foo_type because you are not allowed ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Linked List Help please
    ... iterators are generalized pointers that provide access to the ... typedef listPeople; ... void with_for_loop(People const & people) ...
    (comp.lang.cpp)
  • Const Qualifier question
    ... there are numerous functions that has been defined with CONST ... DATA_TYPE is some typedef ... function that has Const qualifier. ... void init_data_type; ...
    (comp.lang.cpp)