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

David Blaikie dblaikie at gmail.com
Sun Aug 24 14:42:10 PDT 2014


On Aug 24, 2014 1:06 PM, "Jacob Carlborg" <doob at me.com> wrote:
>
> On 2014-08-23 22:42, David Blaikie wrote:
>>
>> 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.

>
>
> --
> /Jacob Carlborg
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140824/9fbbac49/attachment.html>


More information about the cfe-dev mailing list