Re: const fixes seek commit approval
- From: Tim Janik <timj gtk org>
- To: gtk-devel-list redhat com
- Subject: Re: const fixes seek commit approval
- Date: Wed, 28 Oct 1998 17:21:39 +0100 (CET)
On Wed, 28 Oct 1998, Aaron Digulla wrote:
> Quoting Havoc Pennington (rhpennin@midway.uchicago.edu):
>
> > > nope, don't change such occourancs to const gchar**, the same reasons as
> > > for normal return values do apply here as well, it's even a little more
> > > serious.
> >
> > The difference is that in the return value case, if you do it the right
> > way:
> >
> > const gchar* string;
> > string = gtk_foo();
> >
> > you get no warning. But if you do this:
> >
> > const gchar* string;
> > gtk_label_get(label, &string); /* traditional non-const gtk_label_get */
> >
> > it produces the same "incompatible pointer type" warning. So while the
> > non-const return value is basically harmless for people using const
> > properly, in this case you are *required* to cause warnings for either the
> > people using const or the people not using const. There is no way around
> > screwing one of them.
>
> In this case, I'd say thet gtk_label_get() has a wrong prototype: You
> wouldn't want that the user changes the label via string, will you ?
of course not, but that should better be expressed in comments.
a user is supposed to
gchar *string;
gtk_label_get (label, &string);
string = g_strdup (string);
if he wants to further operate on that string.
but requirering an extra variable
gchar *string;
const gchar *const_string;
gtk_label_get (label, &const_string);
string = g_strdup (const_string);
just for the retrival is actually a hassle, especially for code blocks
that are somewhat bigger than the above.
there's still the possibility to do the above with the beforementioned
(gchar**) cast to save the const variable, but that is less then elegant
and the reasons for avoiding this cast still hold (actually, a good programer
attempts to reduce casts as much as possible).
> > > as an aside, the compiler warning produced in such a case goes along the
> > > lines of
> > > ...passing argument 1 of `foo' from incompatible pointer type...
> > > not even mentioning that it's actually const-issue that's going on here.
> > True, this is a vague warning. Though it is technically true, and
> > const is technically part of the type, not a separate thing. And it would
> > probably be good for many C programmers to have to learn this... ;-)
>
> Well, this is not the fault of the patch but of GCC. GCC doesn't have
> meaningful error messages (example: The famous "Parser error before"
> if you use an unknown type).
that's certainly right, but already a little besides the point. thing is, if
gtk doesn't behave truely const enforcing (thus returning const gchar* from
all functions that don't duplicate a string), it is a pain in the ass if we
do that for selected functions only, i.e. those that return strings through
a gchar**.
---
ciaoTJ
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]