Unshared

Removes the outer layer of shared from type T.

If shared has not been applied to the outer layer of type T, then the result is T.

Note that while immutable is implicitly shared, it is unaffected by Unshared. Only explicit shared is removed.

For the built-in scalar types (that is bool, the character types, and the numeric types), they only have one layer, so shared U simply becomes U.

Where the layers come in is pointers and arrays. shared(U*) becomes shared(U)*, and shared(U[]), becomes shared(U)[]. So, a pointer goes from being fully shared to being a mutable pointer to shared, and a dynamic array goes from being fully shared to being a mutable dynamic array of shared elements. And if there are multiple layers of pointers or arrays, it's just that outer layer which is affected - e.g. shared(U**) would become shared(U*)*.

For user-defined types, the effect is that shared U becomes U, and how that affects member variables depends on the type of the member variable. If a member variable is explicitly marked with shared, then it will continue to be shared even after Unshared has stripped shared from the containing type. However, if shared was on the member variable only because the containing type was shared, then when Unshared removes the qualifier from the containing type, it is removed from the member variable as well.

Also, Unshared has no effect on what a templated type is instantiated with, so if a templated type is instantiated with a template argument which has a type qualifier, the template instantiation will not change.

template Unshared (
T
) {}

Examples

1 static assert(is(Unshared!(                   int) == int));
2 static assert(is(Unshared!(             const int) == const int));
3 static assert(is(Unshared!(       inout       int) == inout int));
4 static assert(is(Unshared!(       inout const int) == inout const int));
5 static assert(is(Unshared!(shared             int) == int));
6 static assert(is(Unshared!(shared       const int) == const int));
7 static assert(is(Unshared!(shared inout       int) == inout int));
8 static assert(is(Unshared!(shared inout const int) == inout const int));
9 static assert(is(Unshared!(         immutable int) == immutable int));
10 
11 // Only the outer layer of shared is removed.
12 // shared(int[]) -> shared(int)[]
13 alias SharedIntArr = shared(int[]);
14 static assert(is(Unshared!SharedIntArr == shared(int)[]));
15 
16 // Only the outer layer of shared is removed.
17 // shared(int*) -> shared(int)*
18 alias SharedIntPtr = shared(int*);
19 static assert(is(Unshared!SharedIntPtr == shared(int)*));
20 
21 // shared(int)* -> shared(int)*
22 alias PtrToSharedInt = shared(int)*;
23 static assert(is(Unshared!PtrToSharedInt == shared(int)*));
24 
25 // immutable is unaffected
26 alias ImmutableArr = immutable(int[]);
27 static assert(is(Unshared!ImmutableArr == immutable(int[])));
28 
29 static struct S
30 {
31     int* ptr;
32     const int* cPtr;
33     shared int* sPtr;
34 }
35 
36 shared S s;
37 static assert(is(typeof(s) == shared S));
38 static assert(is(typeof(typeof(s).ptr) == shared int*));
39 static assert(is(typeof(typeof(s).cPtr) == const shared int*));
40 static assert(is(typeof(typeof(s).sPtr) == shared int*));
41 
42 // For user-defined types, if shared is applied to a member variable only
43 // because the containing type is shared, then shared is removed from that
44 // member variable, but if the member variable is directly marked as shared,
45 // then it continues to be shared.
46 
47 // shared S -> S
48 static assert(is(Unshared!(typeof(s)) == S));
49 static assert(is(typeof(Unshared!(typeof(s)).ptr) == int*));
50 static assert(is(typeof(Unshared!(typeof(s)).cPtr) == const int*));
51 static assert(is(typeof(Unshared!(typeof(s)).sPtr) == shared int*));
52 
53 static struct Foo(T)
54 {
55     T* ptr;
56 }
57 
58 // The qualifier on the type is removed, but the qualifier on the template
59 // argument is not.
60 static assert(is(Unshared!(shared(Foo!(shared int))) == Foo!(shared int)));
61 static assert(is(Unshared!(Foo!(shared int)) == Foo!(shared int)));
62 static assert(is(Unshared!(shared(Foo!int)) == Foo!int));

Meta