bug-gnulib
[Top][All Lists]
Advanced

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

Re: gl_omap.h: why does the dispose function expect a CONST void*?


From: Bruno Haible
Subject: Re: gl_omap.h: why does the dispose function expect a CONST void*?
Date: Mon, 19 Sep 2022 21:41:06 +0200

Hi Vivien,

> In gl_omap.h, the function to delete a key or a value from an omap is
> declared as:
> 
> typedef void (*gl_mapkey_dispose_fn) (const void *key);
> typedef void (*gl_mapvalue_dispose_fn) (const void *value);
> 
> They are always called on a value marked as const void *.
> 
> Why is it the case?

1) because in C, when you pass a pointer to an API, and that pointer
   can be either of 'const void *', 'void *', 'const foo *', 'foo *',
   it should best be declared as 'const void *'.

2) because the container implementation (gl_*omap.c) will not modify
   the key nor the value.

> I will have to call free() on them, or similar
> destructors, that aren’t expected to respect the const qualifier.

Yes, in these places, you are supposed to cast away the 'const'. You
created the objects, therefore you know to what actual pointer you can
cast.

> I did not check for the other collection implementations, maybe they
> have the same problem.

Yes, they are designed in the same way.

> Am I expected to cast away the const and call my destructors on the key
> and value?

Yes.

Bruno






reply via email to

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