Date: Sat, 15 Feb 2025 22:54:48 +0000
Yes.
Code like this would be allowed:
for( size_t i = 1; i < vect.size(); ++i)
{
const i;
// i unmodifiable in this body but can still be used to iterate
}
Getting a const reference trick is something I’ve considered, but in your example, there would still be a modifiable ‘i_ctor’ that can be used to modify the value. Making that const instead, there wouldn’t be any other accessible name other than the one you made const.
From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of Sebastian Wittmeier via Std-Proposals
Sent: Saturday, February 15, 2025 11:39 PM
To: std-proposals_at_lists.isocpp.org
Cc: Sebastian Wittmeier <wittmeier_at_projectalpha.org>
Subject: Re: [std-proposals] Delayed const declaration
So in a looped scope the variable would be cost const and non-const in each itetation?
AFAIK this is different from relocation, where the variable stays forbidden to be used again.
Also one alternative would be to create a const reference and only use that:
int i_ctor = 0;
if (b)
i_ctor = 2;
const int& i = i_ctor;
-----Ursprüngliche Nachricht-----
Von: Tiago Freire via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>>
Gesendet: Sa 15.02.2025 23:05
Betreff: [std-proposals] Delayed const declaration
An: std-proposals_at_[hidden]ocpp.org<mailto:std-proposals_at_[hidden]>;
CC: Tiago Freire <tmiguelf_at_[hidden]<mailto:tmiguelf_at_[hidden]>>;
Hi everyone,
I have hinted at this in another thread, what I want to propose is “delayed const declarations”. I.e. variables that are originally not declared const can be made const for the remainder of the scope.
-> Why?
We have all dealt with a case where we have an object that is awkward to initialize.
We want it to protect it with const to make sure it is no longer modified (except for its initial setup), but because of the awkward way that they start you can’t really declare const.
Ex.
errc awkward_init(uint32_t&)
uint32_t var;
awkward_init(var);
if( var == 0 )
{
var = 1;
}
//I want it to be const here
-> Syntax
Not settled yet, I’m considering alternatives to specify this.
My current preferred way is:
const variable_name; //no type use, type of variable_name declared previously
i.e.
uint32_t var;
awkward_init(var);
if( var == 0 )
{
var = 1;
}
const var; //delayed const declaration
var = 3; //error: var is const at this point in this scope
I don’t think is perfect, it looks a lot like a new declaration, but technically should be fine. I’m open to suggestions.
-> Corner cases
I’ve saying making it const for the “duration of the scope” instead of just “simply become const”. This is important.
Take the following case:
uint32_t var;
if(condition)
{
var =1;
const var;
}
else
{
var = 2;
}
var = 3; //Is it const here?
Because the compiler can’t know which of the paths was taken (this is runtime information), it becomes ambiguous in the example above.
So a delayed const declaration can only last for the duration of the scope in which they were declared const.
The following would be valid code:
uint32_t var;
var = 0; //ok
{
var = 1; //ok
const var;
//var = 2; //Not allowed
}
var = 3;// Ok
That’s everything.
I would welcome some feedback into the proposal before I start writing the paper.
And if would you like to help write the wording, that help would be appreciated.
Br,
Tiago
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]g<mailto:Std-Proposals_at_[hidden]>
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Code like this would be allowed:
for( size_t i = 1; i < vect.size(); ++i)
{
const i;
// i unmodifiable in this body but can still be used to iterate
}
Getting a const reference trick is something I’ve considered, but in your example, there would still be a modifiable ‘i_ctor’ that can be used to modify the value. Making that const instead, there wouldn’t be any other accessible name other than the one you made const.
From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of Sebastian Wittmeier via Std-Proposals
Sent: Saturday, February 15, 2025 11:39 PM
To: std-proposals_at_lists.isocpp.org
Cc: Sebastian Wittmeier <wittmeier_at_projectalpha.org>
Subject: Re: [std-proposals] Delayed const declaration
So in a looped scope the variable would be cost const and non-const in each itetation?
AFAIK this is different from relocation, where the variable stays forbidden to be used again.
Also one alternative would be to create a const reference and only use that:
int i_ctor = 0;
if (b)
i_ctor = 2;
const int& i = i_ctor;
-----Ursprüngliche Nachricht-----
Von: Tiago Freire via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>>
Gesendet: Sa 15.02.2025 23:05
Betreff: [std-proposals] Delayed const declaration
An: std-proposals_at_[hidden]ocpp.org<mailto:std-proposals_at_[hidden]>;
CC: Tiago Freire <tmiguelf_at_[hidden]<mailto:tmiguelf_at_[hidden]>>;
Hi everyone,
I have hinted at this in another thread, what I want to propose is “delayed const declarations”. I.e. variables that are originally not declared const can be made const for the remainder of the scope.
-> Why?
We have all dealt with a case where we have an object that is awkward to initialize.
We want it to protect it with const to make sure it is no longer modified (except for its initial setup), but because of the awkward way that they start you can’t really declare const.
Ex.
errc awkward_init(uint32_t&)
uint32_t var;
awkward_init(var);
if( var == 0 )
{
var = 1;
}
//I want it to be const here
-> Syntax
Not settled yet, I’m considering alternatives to specify this.
My current preferred way is:
const variable_name; //no type use, type of variable_name declared previously
i.e.
uint32_t var;
awkward_init(var);
if( var == 0 )
{
var = 1;
}
const var; //delayed const declaration
var = 3; //error: var is const at this point in this scope
I don’t think is perfect, it looks a lot like a new declaration, but technically should be fine. I’m open to suggestions.
-> Corner cases
I’ve saying making it const for the “duration of the scope” instead of just “simply become const”. This is important.
Take the following case:
uint32_t var;
if(condition)
{
var =1;
const var;
}
else
{
var = 2;
}
var = 3; //Is it const here?
Because the compiler can’t know which of the paths was taken (this is runtime information), it becomes ambiguous in the example above.
So a delayed const declaration can only last for the duration of the scope in which they were declared const.
The following would be valid code:
uint32_t var;
var = 0; //ok
{
var = 1; //ok
const var;
//var = 2; //Not allowed
}
var = 3;// Ok
That’s everything.
I would welcome some feedback into the proposal before I start writing the paper.
And if would you like to help write the wording, that help would be appreciated.
Br,
Tiago
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]g<mailto:Std-Proposals_at_[hidden]>
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-02-15 22:54:52