Re: const qualifier in function arguments



Tim Lorenz <tim lorenz nu> writes:
> 
> i don't understand gtk+'s (and related libs) policy of handling
> const qualifiers in function arguments. For example there is:
> 
> gtk_widget_modify_font(GtkWidget* ,PangoFontDescription* fn)
> 
> in gtk-1.3 tree. That function actually doesn't modify 'fn'
> and i think it isn't supposed to. Why isn't this expressed
> through a const qualifier?
> 

In C, const is only useful for some subset of structs, those with:

 - no "mutable" members (because with const, the struct could be 
   in read-only memory)
 - no pointer members, because const doesn't "transfer" to them

This is e.g. why GList is not const:

const GList *list1 = some_list;
GList *list2;

list2 = some_list->next; /* no compiler warning will be given here */

Also, const in C is broken for double-indirected pointers:

 void foo (const char **array);

  char **myarg;
 
  foo (myarg); /* compiler warning will be given */

Anyway, since const isn't good for much in C due to lack of language
support with e.g. const accessor functions, "mutable" keyword, etc.,
there isn't much attempt to care about it by most C
programmers/libraries. The const keyword just doesn't work in C for
anything except Plain Old Data, i.e. things you can copy by value and
have in read-only memory.

PangoFontDescription is probably such a case, and const is maybe a
good idea for gtk_widget_modify_font(). But people forget to use const
since cases where you can do it are rare.

That's the policy as I'd write it, anyway. ;-)

Havoc




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