C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Delayed const declaration

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Sun, 16 Feb 2025 14:16:59 +0000
I have considering alternative syntax, and attribute among them.
While attribute is something in the right direction, it doesn’t get us there.
Because for 1 attributes can be ignored.
But I want if there’s a method to call
For example: x.method(); then the const version of that method should be picked, something you wouldn’t be able to do with an attribute.


From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of Sebastian Wittmeier via Std-Proposals
Sent: Sunday, February 16, 2025 2:59 PM
To: std-proposals_at_[hidden]
Cc: Sebastian Wittmeier <wittmeier_at_[hidden]>
Subject: Re: [std-proposals] Delayed const declaration


You use an attribute-like syntax now (probably as placeholder).





However, that would be an actual alternative:



Create an attribute that the compiler (may) give a warning or an error, if the variable is modified.

Advantage: Standard syntax. Ignorable.





Small issues:



 - Some entity has an attribute. Attributes do not stand alone. One could just give the variable name (l-value) as an expression:



int x = 0;

x = 1;

[[const]] x;



- Attributes can be ignored. Overload resolution would still see the non-const x (to keep the attribute ignorable). Solution: Instead of actually making the variable const, call it



[[warn-if-modified]] x;


-----Ursprüngliche Nachricht-----
Von: Bo Persson via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>>
Gesendet: So 16.02.2025 12:54
Betreff: Re: [std-proposals] Delayed const declaration
An: std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>;
CC: Bo Persson <bo_at_bo-persson.se<mailto:bo_at_[hidden]>>;
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_lists.isocpp.org>
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-02-16 14:17:02