[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: iterator and const iterator

From: Bruno Haible
Subject: Re: iterator and const iterator
Date: Sun, 31 May 2020 12:15:02 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-177-generic; KDE/5.18.0; x86_64; ; )

Hi Akim,

In <https://lists.gnu.org/archive/html/bug-bison/2020-05/msg00102.html> you
> > Le 3 mai 2020 à 17:17, Bruno Haible <bruno@clisp.org> a écrit :
> > 
> > There are a couple of GCC warnings on IRIX (CC="gcc -mabi=n32"):
> > 
> > 2)
> > ../src/fixits.c: In function `fixits_run':
> > ../src/fixits.c:129: warning: dereferencing type-punned pointer will break 
> > strict-aliasing rules
> > 
> > Modern C rules wants f to be a 'const void *'. Inside the loop body,
> > you can cast f to 'fixit const *' without danger.
> So I had installed this:
> -  fixit const *f = NULL;
> +  void const *p = NULL;
>    gl_list_iterator_t iter = gl_list_iterator (fixits);
> -  while (gl_list_iterator_next (&iter, (const void**) &f, NULL))
> +  while (gl_list_iterator_next (&iter, &p, NULL))
>      {
> +      fixit const *f = p;
> Today we have more occurrences of this pattern that we have to fix,
> but some of these items need the pointer to point to a non-const value,
> so we need a const void * pointer, and still a cast.
> Would you accept a macro, say gl_list_mutable_iterator_next, where we
> would pass a void** instead of const void**?

No, I don't want this. It's a madness, in C++, to have to write the same
member function definition twice, due to 'const' differences. This madness
should not propagate into our C code. In C, we don't need to have two
functions which produce the exactly same machine code. C has sufficient
means for guaranteeing that one copy of the code will do.

> Of course mutable_iterator
> gives the impression that it's the iterator which is mutable, but an
> iterator must so obviously be mutable that I don't think it would be
> much of an issue, and it's quite symmetrical with const_iterator.
> It might have been less surprising to have the current gl_list_iterator_next
> be about mutable values and use gl_list_const_iterator_next for readonly
> iteration, but it's too late.

Whether you call it mutable_iterator vs. iterator, or iterator vs.
const_iterator, doesn't change the fact that it's a code duplication.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]