-Re: C API question
Marshall McMullen 2013-07-31, 17:25
IMO I think this is correct behavior from zookeeper's perspective. By
marking the parameter as a const void* the C API is promising that *it*
will not modify the contents of memory referred to by the pointer. That is
absolutely the correct contract for zookeeper to make. If it were not const
then the C API would not be able to guarantee that. Moreover, as a client
building on top of the C API looking at the C API I can easily tell looking
at these functions that it is promising not not modify that data. I don't
see any problems with this.
As to the problem in your callback, that is easily solvable since you own
the memory in the callback so you know you can safely const-cast away the
const. You mentioned reinterpet_cast, so in c++ this would be:
void* data = const_cast<void *>(data_ptr);
On Wed, Jul 31, 2013 at 10:24 AM, Marios Hadjieleftheriou
> Hi Mohammad.
> The parameter is a const void * (reading from right to left: a pointer
> to a void that is const). Hence the data pointed to cannot be changed.
> Moreover, it does not make sense to pass a void* const (right to left:
> a const pointer to a void) as a function argument, because the pointer
> itself ("data" in this case) is passed by value, hence it is
> meaningless for it to be const or not.
> In any case, in my callback function I tried to reinterpret_cast my
> data to a non-const instance and the compiler complained that it needs
> to be const. I cannot change my data.
> On Wed, Jul 31, 2013 at 12:55 AM, Mohammad Shamma
> <[EMAIL PROTECTED]> wrote:
> > I think the const in this case would cause the pointer to be fixed. The
> > data referred to by the pointer can still be updated.
> > This is a similar question on stackoverflow:
> > http://stackoverflow.com/questions/6407041/constant-pointer
> > On Tue, Jul 30, 2013 at 7:06 PM, Marios Hadjieleftheriou
> > <[EMAIL PROTECTED]>wrote:
> >> The string_completiion_t callback signature of the Zookeeper C API is:
> >> typedef void(* string_completion_t)(int rc, const char *value, const
> >> *data)
> >> That means that when the callback is called, the user provided data is
> >> returned as a const void and cannot be modified by client code, which
> >> kind of defeats the purpose of passing any data in the first place.
> >> Is there any reason why this parameter needs to be const?
> > --
> > Mohammad Shamma