[LLVMdev] Can we assume const parameter and to-be-modified parameter do not alias?

Tim Northover
Fri, 09 Jan 2015 22:58:36 -0000

Hi Yi,

On 9 January 2015 at 14:21, yi_jiang <yjiang at apple.com>
wrote:
> Our understanding is that it is user’s responsibility to guarantee that
const array will not be touched in any way and if not the behavior is undefined
and in this case it is safe to assume that they are not alias?

I wouldn't have said so. Const doesn't guarantee an object won't be
modified via a different route in C or C++ (which is one of the
reasons it's often criticised as being a bit too weak). I think const
gives you two things:

+ If you attempt to modify an object *using a const-qualified type*
the compiler must diagnose a problem (it's not a modifiable lvalue in
6.5.16)
+ An underlying object declared const cannot be modified by casting
that away (it's undefined behaviour).

It would be perfectly valid to form (say) a const pointer to a
non-const object and pass that around though; and also perfectly valid
to either pass another (non-const) pointer to that object around in
parallel or even to cast away that constness and modify the object.

Cheers.

Tim.