www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to create a local copy of shared pointer?

reply "denizzzka" <4denizzz gmail.com> writes:
void main()
{
     struct S { int payload; }

     S* s = new shared (S); // Why this is a illegal?
}


Error: cannot implicitly convert expression (new shared(S)) of 
type shared(S)* to S*
Oct 15 2012
next sibling parent "thedeemon" <dlang thedeemon.com> writes:
On Monday, 15 October 2012 at 15:15:57 UTC, denizzzka wrote:
     S* s = new shared (S); // Why this is a illegal?
 Error: cannot implicitly convert expression (new shared(S)) of 
 type shared(S)* to S*

Because shared(S) and S are different types. Either declare s as shared too or use a cast.
Oct 15 2012
prev sibling next sibling parent "denizzzka" <4denizzz gmail.com> writes:
On Monday, 15 October 2012 at 15:27:03 UTC, thedeemon wrote:
 On Monday, 15 October 2012 at 15:15:57 UTC, denizzzka wrote:
    S* s = new shared (S); // Why this is a illegal?
 Error: cannot implicitly convert expression (new shared(S)) of 
 type shared(S)* to S*

Because shared(S) and S are different types. Either declare s as shared too or use a cast.

Why it was made in the language? This can be a safe automatic conversion I think.
Oct 15 2012
prev sibling next sibling parent "thedeemon" <dlang thedeemon.com> writes:
On Monday, 15 October 2012 at 15:34:43 UTC, denizzzka wrote:

 Because shared(S) and S are different types. Either declare s 
 as shared too or use a cast.

Why it was made in the language? This can be a safe automatic conversion I think.

Theoretically compiler can work with shared and non-shared types very differently, with the latter it can do all the work in registers without always updating the memory because it knows no other threads will look at this data. Shared data is much more volatile. So actually not only automatic but also explicit cast should be illegal. I don't know how much differently the compiler deals with these types right now though. Currently the whole story of separating shared and local types is not fully thought, imho, we need some kind of shared-polymorphism, without it we have to use casts quite often and it's not a right thing to do.
Oct 15 2012
prev sibling parent "denizzzka" <4denizzz gmail.com> writes:
Thanks!
Oct 15 2012