[cfe-dev] const stripped in typedefs for parameters of primitive type?

James Dennett james.dennett at gmail.com
Mon Aug 25 00:06:55 PDT 2014


On Sun, Aug 24, 2014 at 11:49 PM, Jacob Carlborg <doob at me.com> wrote:
> On 24/08/14 23:42, David Blaikie wrote:
>
>> 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.
>
>
> 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



More information about the cfe-dev mailing list