g_object_ref and -Wincompatible-pointer-types

I proposed a change to GObject that was merged not too long ago that uses __typeof__ on GCC/Clang to propagate the pointer type from the parameter. For example, the old prototype was something like:

<br />
gpointer g_object_ref (gpointer instance);<br />

Which meant you got a cast for free even if you made a mistake such as:

<br />
FooBar *bar = foo_bar_new ();<br />
FooFrob *frob = g_object_ref (bar);<br />

But now, we propagate the type of the parameter as the return value with a macro that is essentially:

<br />
#define g_object_ref(Obj)  ((__typeof__(Obj)) (g_object_ref) (Obj))<br />

Now, the code example above would be an -Wincompatible-pointer-types warning from GCC/Clang. This isn’t a common problem (people often rely on this implicit cast when using inheritance). However, when you do make the mistake it’s rather annoying to track down. I’ve certainly made the mistake before when too-quickly copying lines of code, and I know I’m not the only one.

So, how do you fix the warnings in your code base if you were relying on the implicit cast? Both of these will work, I have a preference for the former.

<br />
FooFrob *frob = g_object_ref (FOO_FROB (bar));<br />
FooFrob *frob = FOO_FROB (g_object_ref (bar));<br />