HeadUnshared in core.atomic

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 12 03:22:31 PDT 2014


On 6/12/14, 1:29 AM, Mike Franklin wrote:
> Hello,
>
> I was recently exposed to this template in core.atomic:
>
> private
> {
>      template HeadUnshared(T)
>      {
>          static if( is( T U : shared(U*) ) )
>              alias shared(U)* HeadUnshared;
>          else
>              alias T HeadUnshared;
>      }
> }
>
> Could someone please explain/elaborate on what this is doing, and why
> it's necessary and used so often in core.atomic?
>
> Thanks,
> Mike

Hello Mike,

As to why it's necessary, I cannot really say. My only guess is that it 
will be used at a site that requires a pointer to shared data. Anyway, 
it simply observes the template parameter at compile time by creating a 
local variable U (or is U type?) and checking to see if it is a shared 
pointer. If it is, then it produces a pointer to the shared data:

	shared(U)* HeadUnshared;

Note: in this re-designation, the pointer is not shared, just the data 
to which it points.

Otherwise, it simply passes on the type:

	alias T HeadUnshared;

For example if you instantiate with uint:

	HeadUnshared!uint p1;

Then p1 is now a variable of type uint. The type is just forwarded. This 
would be the exact thing as:

	uint p1;

However, if you use a shared pointer:

	HeadUnshared!(shared(uint*)) p2;

The p2 is now a pointer to shared(uint).

Also note that if you just pass a shared(type), you will get the same 
shared(type) in return.

Hope that helps... someone more knowledgeable may be able to explain better.




More information about the Digitalmars-d-learn mailing list