DMD 0.170 release

Tom S h3r3tic at remove.mat.uni.torun.pl
Tue Oct 17 21:25:29 PDT 2006


Bill Baxter wrote:
> In C++ I would just make it a reference parameter, and take the address:
> 
> void print_addr( IntArrayType& arg )
> {
>     writefln(&arg);
> }
> void main()
> {
>     int[] arr = [1,2,3,4,5];
>     writefln(&arr);
>     print_addr(arr);
> }
> 
> Then aside from the fact that D is not C++, it would print the same 
> value for both, and it wouldn't be accessing a static variable.  And it 
> would be perfectly thread safe.


Heh, that's not the same case. You can do that it D, except that you'd 
use inout for the reference. The problem arises when you try to return a 
delegate literal from a function, it then references stack data which is 
invalid after the function returns. Speaking of C++ analogies, it is 
just like returning pointers to stack data. Most compilers complain 
about it because it's simply wrong. There is one solution - actually 
embed that data in the returned value - just what I did with the struct. 
An alternative that I dont like very much would be to create a local 
class and an object thereof, then return a delegate pointing to a method 
of that object. It works, because objects live on the heap. It might be 
shorter implementation-wise than my previous solution, but it involves 
an object allocation and unnecessarily bothers the GC.

If you want to use objects nevertheless, iirc an 'AdvancedDelegate' 
class once announced in this NG allowed a shortcut for this. My Bind 
library ( http://www-users.mat.uni.torun.pl/~h3r3tic/bind.rar ) could 
also be used to associate partial parameters with a function or a 
delegate. But it still requires an object.



More information about the Digitalmars-d-announce mailing list