Date: Sun, 16 Feb 2025 14:13:41 +0000
It is not allowed to cast away const.
What it means is from that point on (and for the duration of the scope), that variable is const (as if it had been the case all along).
In that case UsePointer(&var);would not be allowed because it would cast away const.
From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of organicoman via Std-Proposals
Sent: Sunday, February 16, 2025 1:56 PM
To: Bo Persson via Std-Proposals <std-proposals_at_[hidden]>
Cc: organicoman <organicoman_at_yahoo.fr>; Bo Persson <bo_at_[hidden]>
Subject: Re: [std-proposals] Delayed const declaration
Tiago,
When you delay declare your variable as const, for the rest of the code, how can it know that it should use the const version of the non const?
Exple
void UsePointer(int*);
void foo()
{
int var = init();
.../// do something with var
const var;
++var; // should fail right?
UsePointer(&var);// cast away const???
}
Sent from my Galaxy
-------- Original message --------
From: Bo Persson via Std-Proposals <std-proposals_at_[hidden]socpp.org<mailto:std-proposals_at_[hidden]>>
Date: 2/16/25 12:54 PM (GMT+01:00)
To: std-proposals_at_[hidden]cpp.org<mailto:std-proposals_at_[hidden]>
Cc: Bo Persson <bo_at_[hidden]<mailto:bo_at_[hidden]>>
Subject: Re: [std-proposals] Delayed const declaration
On sön 2025-02-16 at 12:08, Tiago Freire via Std-Proposals wrote:
> If you are going to do that, perhaps we can simplify the syntax a little
> bit.
>
> Instead of having to declare [mutable const] which makes the variable
> open for writes like any other regular variable.
>
> We could just omit [mutable const] and deduce that after the fact
> depending on either or not a close statement is used, instead of:
>
> [mutable const] int x = 0;
>
> const x;
>
> you would simplify it as:
>
> int x = 0;
>
> const x;
>
The two-step declaration has the disadvantage that it invites typos. Or
when you change the name of x, you might forget the const declaration
half a page further down. Now some other x is const?
This reminds me of a similar problems with goto, where added code before
and after the label affects what the goto means (in a totally different
location).
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]<mailto:Std-Proposals_at_[hidden]>
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-02-16 14:13:45