C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A new C++ keyword: forget

From: Jonathan Grant <jgrantonline_at_[hidden]>
Date: Sun, 16 Aug 2026 20:18:07 +0100
On 16/08/2026 16:39, Henry Skoglund wrote:
> On 2026-08-13 23:57, Jonathan Grant wrote:
>>
>> On 09/08/2026 17:29, Henry Skoglund via Std-Proposals wrote:
>>> Hi, for a long time now in C++ we have the const keyword, which disallows writing over a variable.
>>>
>>> What if we also want to disallow *reading* a variable?
>> Henry
>>
>> Would my compile-time Forget meet your needs? it works at compile-time
>> It does this using P4021 compile_assert
>>
>> β€˜int main()’ at main.cpp:17:25:
>> forget.hpp:26:9: error: call to β€˜_compile_assert_fail_0’ declared with attribute error: Forget: cannot access a forgotten value
>>
>>
>> https://github.com/jonnygrant/compile_assert/tree/main/experiments/forget
>>
>> It's just an example to show how to prevent access, not production code.
>> Which compiler are you using?
>>
>> Kind regards
>> Jonathan
>>
> Hi Jonathan, I tried your compile-time Forget on GCC 13.3.0 and it works fine. But it's tricky with scopes, for example:
> ...
> Forget<int> a(42);
>
> if (rand() % 2)
> {
> // remove access to 'a'
> Forget<int> b(a);
>
> int try_to_access = a;
> }
>
> int try_to_access2 = a;
> ...
> Ideally I should get an error message like 'Not all control paths lead to a Forget' or if we allow only Forgets in the same scope, an error like 'A Forget has to be in the same scope as the Forgetee'.
>


That's good it could work in your software. I'm always pleased to hear of it being useful. Maybe you can suggest a change to the forget.hpp to handle this?

Compilers could put in more advanced diagnostic messages if they progress and support the proposal.


I put it online here. It's possible to re-enable access after the scope:

https://godbolt.org/z/7dK13bP6z


compile__assert verifies


Yes. Conceptually, compile_assert(expression) has three possible analysis results:

Result Meaning Outcome
Pass proven true on reachable code
Fail proven false on at least one reachable code path Diagnostic / compilation fails
Unknown cannot prove the expression true for every reachable path Diagnostic / compilation fails

Kind regards
Jonathan

Received on 2026-08-16 19:18:15