www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do you get T from shared(T) ?

reply Marco Leise <Marco.Leise gmx.de> writes:
For head-unshared there is `static if (is(T U : shared U))`.
But how do you get the unshared type for anything from `shared
void*` to `shared uint` ?

-- 
Marco
Sep 28 2014
next sibling parent reply "tcak" <tcak gmail.com> writes:
On Sunday, 28 September 2014 at 09:11:07 UTC, Marco Leise wrote:
 For head-unshared there is `static if (is(T U : shared U))`.
 But how do you get the unshared type for anything from `shared
 void*` to `shared uint` ?
shared int a; int b = cast()a;
Sep 28 2014
parent Marco Leise <Marco.Leise gmx.de> writes:
Am Sun, 28 Sep 2014 14:07:22 +0000
schrieb "tcak" <tcak gmail.com>:

 On Sunday, 28 September 2014 at 09:11:07 UTC, Marco Leise wrote:
 For head-unshared there is `static if (is(T U : shared U))`.
 But how do you get the unshared type for anything from `shared
 void*` to `shared uint` ?
shared int a; int b = cast()a;
No, I mean for `shared void*`, too. But I special cased for one level of pointer indirection now, so it works. -- Marco
Sep 28 2014
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Sunday, 28 September 2014 at 09:11:07 UTC, Marco Leise wrote:
 For head-unshared there is `static if (is(T U : shared U))`.
 But how do you get the unshared type for anything from `shared
 void*` to `shared uint` ?
template UnShared(T, bool removeAll = false) { static if(is(T : shared U, U)) { static if(is(U : shared(V)*, V)) { alias UnShared = UnShared!(V, true)*; } else { alias UnShared = U; } } else { static if(removeAll && is(T : U*, U)) { alias UnShared = UnShared!(U, true)*; } else { alias UnShared = T; } } } unittest { static assert(is(UnShared!(shared int) == int)); static assert(is(UnShared!(shared void*) == void*)); static assert(is(UnShared!(shared(int**)*) == shared(int**)*)); static assert(is(UnShared!(shared(int**)**, true) == int****)); }
Sep 30 2014
parent Marco Leise <Marco.Leise gmx.de> writes:
Am Tue, 30 Sep 2014 14:48:03 +0000
schrieb "John Colvin" <john.loughran.colvin gmail.com>:

 On Sunday, 28 September 2014 at 09:11:07 UTC, Marco Leise wrote:
 For head-unshared there is `static if (is(T U : shared U))`.
 But how do you get the unshared type for anything from `shared
 void*` to `shared uint` ?
template UnShared(T, bool removeAll = false) { static if(is(T : shared U, U)) { static if(is(U : shared(V)*, V)) { alias UnShared = UnShared!(V, true)*; } else { alias UnShared = U; } } else { static if(removeAll && is(T : U*, U)) { alias UnShared = UnShared!(U, true)*; } else { alias UnShared = T; } } } unittest { static assert(is(UnShared!(shared int) == int)); static assert(is(UnShared!(shared void*) == void*)); static assert(is(UnShared!(shared(int**)*) == shared(int**)*)); static assert(is(UnShared!(shared(int**)**, true) == int****)); }
Ah, thanks. That does the trick! -- Marco
Oct 01 2014