git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Forward declaration of enum iterator_selection?

From
Ramsay Jones <ramsay@ramsayjones.plus.com>
Date
Aug 7, 2016, 20:34 UTC
Message-ID
<0604cf0a-2b94-93b3-3a01-213ea5b9849b@ramsayjones.plus.com>
In-Reply-To
<933f540f-7752-cfce-5785-b67728fea987@kdbg.org>
On 05/08/16 23:26, Johannes Sixt wrote:
Show 7 quoted lines
> When refs.c is being compiled, the only mention of enum iterator_selection is in this piece of code pulled in from refs-internal.h (have a look at the preprocessed code):
> 
> typedef enum iterator_selection ref_iterator_select_fn(
>         struct ref_iterator *iter0, struct ref_iterator *iter1,
>         void *cb_data);
> 
> This looks like a forward declarations of an enumeration type name, something that I thought is illegal in C. Am I wrong? (That may well be the case, my C-foo is quite rusty.)

At this point 'enum iterator_selection' is an incomplete type and may be used when the size of the object is not required. It is not needed, for example, when a typedef name is being declared as a pointer to, or as a function returning such a type. However, such a type must be complete before such a function is called or defined.

> My compiler does not complain (it's gcc 4.8), but I thought I mention it before someone with a pickier compiler stumbles over it...
So, I think this is correct.

Having said that, I would rather the 'enum iterator_selection' be defined before this declaration. One solution could be to #include "iterator.h" prior to _all_ #include "refs/refs-internal.h" in all compilation units (Note it is in the opposite order in refs/iterator.c). Alternatively, you could put the #include "../iterator.h" into refs/refs-internal.h directly (some people would object to this).

ATB, Ramsay Jones

Previous: Johannes SixtNext: Johannes Sixt
Message 2 of 6 in “Forward declaration of enum iterator_selection?”
  1. Johannes SixtAug 5, 2016
  2. Ramsay JonesAug 7, 2016
  3. Johannes SixtAug 8, 2016
  4. Ramsay JonesAug 8, 2016
  5. Ramsay JonesAug 8, 2016
  6. Michael HaggertyAug 10, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.