In Linux kernel source code, I found this.
int (*check_fb)(struct device *, struct fb_info *);
...
...
...
static int pwm_backlight_check_fb(struct backlight_device *bl,
struct fb_info *info)
{
struct pwm_bl_data *pb = bl_get_data(bl);
//something more
return !pb->check_fb || pb->check_fb(pb->dev, info);
}
I don't understand the last return
statement, what does it mean? And can we return a function? Usually we return a value.
In C++, the
||
operator is a short-circuit one, meaning that, if the first argument is true, it never even evaluates the second, becausetrue || anything
is always true.Hence the statement:
is a short way of checking for a non-NULL function pointer and using that to decide the current return value:
If the function pointer is
NULL
, simply return!NULL
, which will give you1
.If it's not NULL,
!pb->check_fb
will evaluate as0
, so you then have to call the function, and use whatever it returns, as the return value of this function.So it's effectively the same as:
Now I say "effectively" but the actual values returned may be slightly different between what you saw, and my final code snippet above. The effect is the same however, if you're treating them as true/false values.
Technically, that final line should be:
to have exactly the same return value.