On Sat, 5 Dec 2020, 19:08 David Brown, <david.brown@xxxxxxxxxxxx> wrote: > On 05/12/2020 18:29, Andrea Corallo via Gcc-help wrote: > > David Brown <david.brown@xxxxxxxxxxxx> writes: > > > > [...] > > > >> There is no way in C to say that there can't be anything else that > >> changes the object. > > > > I think it would be nice to have, if not in C at least in GCC. > > > > Andrea > > > > Agreed. But I think there are so many possible things about constness > that it would be nice to have, that it could quickly get out of hand. > > I'd really like a way to tell the compiler that a given object gets set > once during some early function in the program, and can never change > after that. But I can't think of any good syntax for that. > An attribute on the assignment statement (or function) that sets it could say it's the last write to the object. That seems appropriate for an attribute, since it's only an optimisation hint and a conforming compiler could choose to ignore the hint without changing the meaning of the code. (That's the rule of thumb for whether to use [[attribute]] syntax in ISO C++).