C++ Logo


Advanced search

Re: [std-proposals] Alteration of elements in a set (non-const iterator)

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 22 May 2023 12:23:42 -0700
On Monday, 22 May 2023 12:05:34 PDT Edward Catmur wrote:
> I want an attribute to say "no, really, I promise not to const_cast".
> Oh, like [[gnu::pure]]? https://gcc.godbolt.org/z/6MevY4aej

Yes for that example, but not in all real-world use-case. That would be useful
for functions that consume the input and return primitives or simple
aggregates as results, like hashing functions, testers like string "find", etc.

But it doesn't go far enough. There are other examples of functions that can
write to other, non-const parameters. The prototypical example of this is
memcpy, which will not modify one of the inputs but will definitely modify the

Going further, still in the string domain, you can think of functions creating
substrings. Those may allocate memory, which is not allowed in a pure
function, but will never modify the input string. And since the string itself
isn't part of the object (in non-SSO scenarios) but is kept as a pointer,
there needs to be a way to advise the compiler that the pointed-to data
remains unchanged too.

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-05-22 19:23:44