Re: GHashTable and const



On Thu, Jul 3, 2008 at 3:49 PM, Alberto Mardegan
<mardy users sourceforge net> wrote:
> ext Havoc Pennington wrote:
>> Whether you agree or not, the GLib types don't use const in their API,
>> so if you try to use const yourself on these types you're just signing
>> up for pain. It won't work well or do anything useful. (If _all_ the
>> methods on an object are non-const, all the const keyword does is make
>> you do a bunch of casts.)
>
> While I tend to agree that const in C is of limited use, I find it a nice
> way to tell the users of an API not to modify/free certain data.
> So, when I am the owner of a GHashTable and I'd like to have others access
> it in read-only mode, I'd use a const modifier on it.
>
> On your past mail, you write that const is not useful in C, but on the other
> hand you don't write that it's harmful either.

It is line-noise. It contributes nothing to the clarity of the code,
because it is already obvious enough that g_hash_table_size and
similar functions doesn't modify the hash table. It makes the API more
annoying to use as you'll get more "discards qualifiers from pointer
target type" unless you add non-const casts when you use const
variables. If you compile some GNOME projects you'll find that that
warning is one of the most common ones.

It is just a lot of work and annoyance for a (until someone profiles
the API with and without const modifiers) totally imaginary speed
benefit.

> To put some more meat into the discussion, I point out that GValue's APIs
> make heavy use of the const modifier, and from the user point of view that's
> very helpful.

In what way?


-- 
mvh Björn


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