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@lists.isocpp.org> 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@hpkfft.com> Sent: Saturday, February 15, 2025 11:27 PMTo: std-proposals@lists.isocpp.orgCc: Tiago Freire <tmiguelf@hotmail.com>Subject: RE: [std-proposals] Delayed const declarationSo 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 listStd-Proposals@lists.isocpp.orghttps://lists.isocpp.org/mailman/listinfo.cgi/std-proposals