Re: iterator and const iterator

Bruno Haible <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs,gmane.comp.parsers.bison.bugs
Message-ID <3359933.ChhfAiNKbc@omega>
Hi Akim,

In <https://lists.gnu.org/archive/html/bug-bison/2020-05/msg00102.html> you
write:
> > Le 3 mai 2020 à 17:17, Bruno Haible <[email protected]> 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.

Bruno
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.