pointers, functions, and uniform call syntax

monarch_dodra monarchdodra at gmail.com
Mon Sep 3 05:13:10 PDT 2012


I was playing around with a very big struct, and told myself I 
wanted it allocated on the heap. This meant I was now 
manipulating S* instead of an S.

I thought "this should have zero impact, because in D, because 
"." is supposed to deference for you if needed."

I find it strange though that when trying to call a function with 
a pointer, the pointer isn't automatically dereferenced if needed:

----
struct S
{
   void foo();
}
void bar(S);
void main()
{
   auto r = new S;
   r.foo();
   bar(r);  //derp
   r.bar(); //derp
};
----
I find it strange, because I thought the entire point was to 
abstract way something was allocated to the way it was used: EG. 
 From a caller perspective, I don't care if r is on the stack or 
on the heap: I just want to call the method bar on the object in 
question.

Why does one consider a "free standing" function more ambiguous 
than a member function?

Things get even stranger if you mix in uniform call syntax. 
"r.foo()" works, but "r.bar()" doesn't?

Am I really forced into:
----
struct S
{
   void foo();
}
void bar(S);
void main()
{
   auto r = new S;
   r.foo();
   bar(*r);    //Groan
   (*r).bar(); //Super Groan.
};
----
I feel as if I'm just back at square 1...


More information about the Digitalmars-d mailing list