The use of const
in a declaration changes the sort of the object
declared.
In Larch/C++ the convention is to use the sort generator ConstObj
instead of Obj
.
However, struct
s and union
s are more complex,
because const
declarations for them mean that their top-level fields
cannot be changed.
Also the usual exception has to be made for arrays,
because arrays are not themselves objects.
For example, consider the following.
Declaration Name Its sort (when used as a global variable) --------------------- ---- ----------------------------------------- const int ci = 7; ci ConstObj[int] const int& rc = ci; rc ConstObj[int] int i = 6; i Obj[int] int& const cr = i; cr Obj[int] const int * pci = &ci; pci Obj[Ptr[ConstObj[int]]] int * const cip = &i; cip ConstObj[Ptr[Obj[int]]] const int cai[3]; cpi Arr[ConstObj[int]] struct IPair { int fst, snd; }; const IPair cp; cp ConstObj[Const[IPair]] union IR { int i; float r; }; const IR ir; ir ConstObj[Const[IR]]
The only reason for not making the sort of ci
be int
is that one can take the address of ci
, whereas one cannot take
the address of an integer value (such as 7).
To get the abstract value, one has to use a state function;
for example, the sort of ci^
and ci'
is int
.
The name rc
is a reference to a constant integer object,
and hence has the same sort as ci
.
The name cr
is a constant reference to an integer object,
but all references are unchanging in any case,
so the use of const
in its declaration is superfluous.
This is why the sort of cr
is the same as the sort of i
.
The distinction between pointers to constants and constant pointers
has more weight than the same distinction for references.
Note that the sorts are different for pci
(a pointer to a constant
integer object) and cip
(a constant pointer to an integer).
The sort of *(pci')
is ConstObj[int]
,
while the sort of *(cip')
is Obj[int]
.
Go to the first, previous, next, last section, table of contents.