const with functions

gcc 4 now throws up a warning about functions defined with const; it appears to be taking the const as a qualifier on the return code.

ianw@lime:/tmp$ cat const.c
static const int const_function(int i)
{
    return i + 100;
}

void call_const_function(void)
{
        int b = const_function(20);
}
ianw@lime:/tmp$ gcc-4.0 -Wall -c const.c
const.c:2: warning: type qualifiers ignored on function return type
const.c: In function 'call_const_function':
const.c:11: warning: unused variable 'b'

When you define a function const you are telling the compiler that you don't examine anything but your arguments (i.e. no globals either) and have no side effects (other than what you return). This allows it to be smarter in compiling the code.

Type qualifiers (like const) are ignored for function return values, for reasons explained here.

Thus the way to indicate to gcc that the function is const is via __attribute__((const)).

Actually, I built two IA64 kernels, one with a const function properly attributed an another with it not and the code wasn't actually any different. But it might be, one day! I am told that in the past when a function is called twice from within the same function code improvements have been quantified.