Definitive list of storage classes

kdevel kdevel at vogtner.de
Fri Jan 29 21:32:19 UTC 2021


On Sunday, 15 April 2012 at 09:59:54 UTC, Peter Alexander wrote:

[...]

> As I understand it, const, immutable, and shared are both type 
> qualifiers and storage classes. In C++, const is also both a 
> storage class and type qualifier, which causes confusion.
>
> // At global scope
> immutable int x = 1;
> int y = 2;
>
> Here x and y are stored very differently. y is thread local 
> whereas x is a program-wide read-only global. It's a similar 
> situation with const and shared.

A few days ago I ran into the following problem:

```mod.d
module mod;

immutable int x = 1;
     const int y = 1;
           int z = 1;
```

```main.d
import std.stdio;
import mod;

void main ()
{
    writeln (x);
    writeln (y);
    writeln (z);
}
```

    $ dmd -c mod.d main.d
    $ dmd main.o mod.o
    $ ./main
    1
    1
    1

This is expected. Now I changed mod.d, recompiled only it and 
relinked:

```mod.d (2nd version)
module mod;

immutable int x = 2;
     const int y = 2;
           int z = 2;
```

    $ dmd -c mod.d
    $ dmd main.o mod.o
    $ ./main
    1
    1
    2

Here I'd expect three 2s. But this behavior I only achieve if I 
create
separate initializations:

```mod.d (3rd version)
module mod;

immutable int x;
     const int y;
           int z = 2;

shared static this () {
    x = 2;
    y = 2;
}
```

Unfortunately I could not find a reasonable recap on this issue. 
In [1]
const and immutable are intruduced as "attributes" but there is no
explanation of their meanings. In [2] const and immutable become
"type qualifiers" and likewise "storage classes".

[1] https://dlang.org/spec/attribute.html#const
[2] https://dlang.org/spec/const3.html


More information about the Digitalmars-d mailing list