www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static fields with shared fields

reply Marcel <marcelpi97 gmail.com> writes:
Hello!
If I declare a static variable inside a struct that contains a 
shared field, is that new variable also shared? For example, say 
I have a custom atomic wrapper type, like C++'s std::atomic<T>, 
that contains only one shared variable: Does it remain shared 
when I instantiate one or do I have to rely on shared again (or 
__gshared)?
Jan 27 2020
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/27/20 5:39 PM, Marcel wrote:
 Hello!
 If I declare a static variable inside a struct that contains a shared 
 field, is that new variable also shared? For example, say I have a 
 custom atomic wrapper type, like C++'s std::atomic<T>, that contains 
 only one shared variable: Does it remain shared when I instantiate one 
 or do I have to rely on shared again (or __gshared)?
I would say the type itself would have to be shared. You can (I think) declare shared types, which are automatically shared even without declaring the variable shared. At least, it used to be that way. Note that anything you declare shared can be shared. So even if you can't share the enclosing struct, you can share the member. -Steve
Jan 27 2020