How to pass a class by (const) reference to C++

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Dec 15 22:24:42 UTC 2021


On Wed, Dec 15, 2021 at 10:01:19PM +0000, Jan via Digitalmars-d-learn wrote:
> On Wednesday, 15 December 2021 at 21:30:47 UTC, Tim wrote:
[...]
> ```cpp
> extern export __gshared static int var;
> ```
[...]
> Joking aside, I understood the docs such that `__gshared` actually
> *is* `static` in D, no?

No. `static` in this case means the lifetime is the lifetime of the
module. By default, a static variable would be in TLS and would have the
lifetime of the thread that it was instantiated in (and there would be
one instance per thread).

`__gshared` is needed to coax the compiler into making the variable
global in the C/C++ sense, i.e., only 1 instance across all threads.

tl;dr: C static == D __gshared.


T

-- 
Marketing: the art of convincing people to pay for what they didn't need before which you fail to deliver after.


More information about the Digitalmars-d-learn mailing list