const stripped in typedefs for parameters of primitive type?

I have a tool that does some processing on C code using libclang. I recently found out that it seems libclang strips const in typedefs for parameters of primitive type. Example:

typedef void (*foo)(const char* a, const int b);
void (*bar)const char* c, const int d);

According libclang, in "foo", "a" is const but "b" is not const. But in "bar", both "c" and "d" are const.

Is this how C behaves, or have I made an mistake how the tool handles typedefs. Or is this a bug in libclang?

I have tried with both libclang 3.1 and 3.5-rc3

This is how C behaves - the const has no effect on the function type there

So a typedef strips const for parameters of a value type? But otherwise it's not stripped, seems odd.

This is how C behaves - the const has no effect on the function type there

So a typedef strips const for parameters of a value type? But otherwise it’s not stripped, seems odd.

Try writing these two functions in one file:

void func(int) {}
void func(const int) {}

And it won’t compile (but if you add * yo each parameter it will) because the functions have the same type.

This is because the const has no effect on the caller - in either case the caller can pass a const or non-const int because it will be copied (not modified) to the caller.

Whereas if there’s indirection it matters greatly yo the caller whether tit gave a pointer through which data could be modified or not.

But this const doesn’t affect the caller (so these are the same function:

void func(int *)
void func(int * const)

Because the pointer (which is copied) is constant, but what it points to is still non-const in both cases.

I understand that the const doesn't matter in this case. But what I don't understand is why libclang will say that in the function above the parameter is const. But if I use a typedef it will not be const.

In the case of a function declaration that is a definition, it makes a
difference:

The simplest example:
void f(int i) { i = 42; }
void g(const int i) { i = 0; } // compilation error.

The type of the parameter `i` differs, though the signatures are the same.

(Of course this is academic if the declaration is not a definition, or
if the parameter is unnamed.)

Function _types_ don't have the top-level const on parameters, but
function _declarations_ can.

-- James

Ok. But if I declare a variable and the type is a function pointer. Does that count as a function declaration or a function type?