This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] warning about const multidimensional array as function parameter
- From: Martin Uecker <uecker at eecs dot berkeley dot edu>
- To: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- Cc: "Joseph S. Myers" <joseph at codesourcery dot com>, gcc Mailing List <gcc at gcc dot gnu dot org>
- Date: Mon, 27 Oct 2014 09:21:47 -0700
- Subject: Re: [PATCH] warning about const multidimensional array as function parameter
- Authentication-results: sourceware.org; auth=none
- References: <20141025123245 dot 4ed09551 at lemur> <Pine dot LNX dot 4 dot 64 dot 1410271254470 dot 13536 at digraph dot polyomino dot org dot uk> <CAH6eHdR7zK5T_P0m15Kg8WXoNKPZ1iD=FjuFUCzCkpuK6DYAjQ at mail dot gmail dot com>
Jonathan Wakely <jwakely.gcc@gmail.com>:
> On 27 October 2014 13:10, Joseph S. Myers wrote:
> > On Sat, 25 Oct 2014, Martin Uecker wrote:
> >
> >> Strictly speaking the C standard considers such pointers to be
> >> incompatible. This seems to be an unintentional consequence
> >> of how qualifiers are always attached to the element type.
> >> (I am trying to get the standard revised too.) The new
> >> behaviour should also be more compatible with C++.
> >
> > What is the exact difference in wording in the C++ standard that results
> > in this difference in semantics?
>
> See 4.4 [conv.qual] in https://isocpp.org/files/papers/N3797.pdf
Note that this doesn't talk explicitly about arrays
and that C++ keeps the notion that qualifiers are
always attached to the element type:
---
3.9.3(2) ... Any cv-qualifiers applied to an array type
affect the array element type, not the array type (8.3.4).
---
and
---
3.9.3(5) ...
Cv-qualifiers applied to an array type attach to the
underlying element type, so the notation âcv T,â where
T is an array type, refers to an array whose elements
are so-qualified. Such array types can be said to be
more (or less) cv-qualified than other types based on
the cv-qualification of the underlying element types.
---
I *believe* (but I don't know the C++ standard very well)
that all the magic is in the wording "can be said to be more
(or less) cv-qualified" which makes the conversion rules work
for arrays with constant element type "as if" the array itself
had the qualifier.
---
4.4 Qualification conversions
4.4(1) "A prvalue of type âpointer to cv1 Tâ can be converted
to a prvalue of type âpointer to cv2 Tâ if âcv2 Tâ is more
cv-qualified than âcv1 Tâ."
---
There is another issue in C which has the same underlying reason
(brought up by Tim Rentsch in comp.std.c) as shown in the following
example (this is legal C and compiles without a warning (gcc) but is
illegal in C++):
#include <string.h>
static const int x[5] = { 0 };
void test(void)
{
memset(&x, 0, sizeof(x));
}
I did not try to address this in the patch because it would make legal
code have a warning, but one could think about it.
Martin
PS: Joseph, thank you for reviewing the patch.