Date: Thu, 13 Aug 2026 22:57:21 +0100
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
>
> One way is to hide it inside a dummy compound statement, like when you're inside a switch statement:
>
> ... break;
> case 42:
> {
> int helper = x + 42;
> ...
>
> but consider a for loop:
> for (int i = 0; (i < 42): ++i)
> {
> }
> no easy way to disallow reading that i variable inside the for loop.
> Same problem also for argument(s) to a function, when inside the function body.
>
>
> Introducing the c++ forget keyword
>
> This lets you specify variables, types or functions that you want to forget/hide in the current scope. E.g. the for loop:
> for (int i = 0; (i < 42): ++i)
> {
> int b = i * i; // ok
> ...
> forget i;
>
> int c = i / 7; // compiler error: i is forgotten and cannot be used
> }
>
> Forgetting a variable does not affect destructors, they run as normal. I.e. the forget keyword is only a compile time thing.
>
>
> You can also forget your least favorite parts of the C++ library, e.g.
> forget strcpy(char*, const char*);
>
> so that you'll get a compiler error whenever you try to use strcpy() in a TU.
>
> Best regards Henry
>
> 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
>
> One way is to hide it inside a dummy compound statement, like when you're inside a switch statement:
>
> ... break;
> case 42:
> {
> int helper = x + 42;
> ...
>
> but consider a for loop:
> for (int i = 0; (i < 42): ++i)
> {
> }
> no easy way to disallow reading that i variable inside the for loop.
> Same problem also for argument(s) to a function, when inside the function body.
>
>
> Introducing the c++ forget keyword
>
> This lets you specify variables, types or functions that you want to forget/hide in the current scope. E.g. the for loop:
> for (int i = 0; (i < 42): ++i)
> {
> int b = i * i; // ok
> ...
> forget i;
>
> int c = i / 7; // compiler error: i is forgotten and cannot be used
> }
>
> Forgetting a variable does not affect destructors, they run as normal. I.e. the forget keyword is only a compile time thing.
>
>
> You can also forget your least favorite parts of the C++ library, e.g.
> forget strcpy(char*, const char*);
>
> so that you'll get a compiler error whenever you try to use strcpy() in a TU.
>
> Best regards Henry
>
Received on 2026-08-13 21:57:31
