Sujet : Re: constexpr keyword is unnecessary
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 20. Oct 2024, 02:56:03
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vf1nvj$83oo$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
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.