| Deutsch English Français Italiano |
|
<vf1nvj$83oo$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Thiago Adams <thiago.adams@gmail.com> Newsgroups: comp.lang.c Subject: Re: constexpr keyword is unnecessary Date: Sat, 19 Oct 2024 22:56:03 -0300 Organization: A noiseless patient Spider Lines: 35 Message-ID: <vf1nvj$83oo$1@dont-email.me> References: <veb5fi$3ll7j$1@dont-email.me> <877ca5q84u.fsf@nosuchdomain.example.com> <vf0ijd$3u54q$1@dont-email.me> <vf0l98$3un4n$1@dont-email.me> <vf1216$p0c$1@dont-email.me> <87y12jpxvl.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 20 Oct 2024 03:56:04 +0200 (CEST) Injection-Info: dont-email.me; posting-host="26ddf3a2db6c9a64a775b4526ef6ea85"; logging-data="266008"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RqFvrYZzan3UzWYBv2ppm6DGF1fFSH/4=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:8SGgQlVwDVPrfdnxBOOFDtgQ9ws= Content-Language: en-GB In-Reply-To: <87y12jpxvl.fsf@nosuchdomain.example.com> Bytes: 2398 >Em 10/19/2024 6:48 PM, Keith Thompson escreveu: > Thiago Adams <thiago.adams@gmail.com> writes: > Let's pretend that when "const" was introduced in C89, it was spelled > "readonly", which more closely reflects its meaning. Would you suggest > that > > readonly int n = 42; > > should make n a constant expression? > I used to find const confusing, as it sometimes meant 'read-only' and other times 'immutable.' Now, it seems less confusing to me. When const is used with variables that can be initialized (init-declarator), it acts as 'immutable,' meaning the storage is constant. In other contexts, like function parameters, const means 'read-only' because we don’t know if the storage is constant or not. It’s also interesting to note that constexpr acts as a storage qualifier. What the compiler needs to know when evaluating an expression at compile time, without depending on flow analysis, is the guarantee that the object is immutable. This makes it safe to use the value it has at initialization when the initialization is also a compile time expression. const used in variables that can be initialized gives the compiler the same guarantees.