C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Delayed const declaration

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Sun, 16 Feb 2025 11:08:00 +0000
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;



I think we should bring to attention that this is a common enough problem that many of you already have a go-to work around (immediate lambda).

What we are trying to achieve is to be lock away a variable from writing after it has been modified, and we want to express the idea “this is now locked for modifications”.
And making it a lambda is just a loop-hole in the language that may in some circumstances allows you to achieve a similar result despite the fact that the language has no mechanism to lock a variable writing after modify.
Instead of just jumping trough hoops, lets just make that a feature of the language.

I’ve also had another example where this is much more interesting than any other alternative;

void foo(uint32_t& var)
{
var = 1; //modify here is fine;
const var;
//var = 3; //Error. No longer modifiable. var is now threated as uint32_t const&
do_something_else(var + 1); //ok to read, this is fine
}





From: Filip <fph2137_at_[hidden]>
Sent: Sunday, February 16, 2025 9:51 AM
To: std-proposals_at_[hidden]
Cc: Paul Caprioli <paul_at_[hidden]>; Tiago Freire <tmiguelf_at_[hidden]>; std-proposals_at_[hidden]
Subject: Re: [std-proposals] Delayed const declaration

Perhaps being a bit more explicit in the syntax:

mutable const int x = 0;
if(cond) {
  x = 1;
} else {
  x = 2;
}
const x;

‘mutable const’ could indicate to both the compiler and the programmer that the const declaration is not finished in a way…
That it will encounter the line in which it stops further modifications of the variable.

Cheers, Filip


Wiadomość napisana przez Tiago Freire via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>> w dniu 15 lut 2025, o godz. 23:42:
Ok, granted my example was a bit simple.
But immediate lambdas are not a solution here.

1. Think of more complex types, and situations where it is not guaranteed NRVO.

Or

2. Situations where the interface is closed (or things like third party libraries with object oriented C type interfaces) (example given initialized by passing reference to a function), but you still want to do error handling. Putting that into a lambda may make your error handling more complicated because your return path is now occupied for the purpose of initializing the object you want to make const.



-----Original Message-----
From: Paul Caprioli <paul_at_[hidden]<mailto:paul_at_[hidden]>>
Sent: Saturday, February 15, 2025 11:27 PM
To: std-proposals_at_lists.isocpp.org<mailto:std-proposals_at_[hidden]>
Cc: Tiago Freire <tmiguelf_at_[hidden]<mailto:tmiguelf_at_[hidden]>>
Subject: RE: [std-proposals] Delayed const declaration


So a delayed const declaration can only last for the duration of the scope in which they were declared const.

That's a disadvantage compared to using a lambda:

   const uint32_t var = [] {
       if (something)
           return 1;
       else
           return 2;
   }();


--
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 11:08:10