Liste des Groupes | Revenir à cl c |
On 28/11/2024 19:58, Keith Thompson wrote:Bart <bc@freeuk.com> writes:>
[...]I think 'const' is confusing for similar reasons that VLAs can be bothSure. For example, given
confusing and awkward to implement.
>
That's because both really apply to /types/, not directly to variables.
const int n = 42;
n is of type `const int`, and &n is of type `consts int*`. Of course
that implies that n itself is const.
But that is a separate thing. Suppose T was an alias for 'const int'. Then:
>
T x; // defines a readonly variable (which probably needs
// initialising)
T* y; // defines a variable pointer
>
'const' is out of the picture.
Other languages tend to have special
keywords that apply to the variable declaration, not the type, for
example:
>
let x:int # non-mutable
var y:int* # mutable (using whatever pointer syntax)
'const' C looks like it works like that, but it doesn't.
There also
examples like this:
>
int const * const p;
>
Here storage for p is allocated, but it it the second 'const' that
makes it readonly. The first 'const' is not involved in allocation at
all. This is easy to get mixed up.
VLAs are mostly linked to stack allocation. But that only applies when
the array is at the top level of the type spec, in the same why that
it's the top-level 'const' that would determine whether storage is
read-only - if declaring a variable.
>
As I said, other languages tend to only have that top-level aspect. I
consider that less confusing. I don't think you'd see multiple 'let'
or 'mut' keywords within one variable declaration.
Les messages affichés proviennent d'usenet.