Do we really need const?

Bruce Adams tortoise_74 at yeah.who.co.uk
Tue Sep 18 17:02:43 PDT 2007


Janice Caron Wrote:

> On 9/18/07, Bruce Adams <tortoise_74 at yeah.who.co.uk> wrote:
> > > Under my suggestion, if the caller passes a struct...
> > >
> > > f(s)
> > >
> > > ...and the callee declares the function as...
> > >
> > > void f(S s)
> > >
> > > ...then all information is known. Both ends must surely know s.sizeof
> > > at compile time? And since that's the /only/ thing the compiler needs
> > > to know to make the decision.
> > >
> > S could be an opaque type so s.sizeof may still be undefined.
> 
> Could you explain further? I don't understand what an "opaque type" is
> in D. Observe that the example I cited above explicitly states that s
> be a struct, not a class.
> 
> Given that the struct would ordinarily have to be built on the stack
> at the caller site, and copied onto the stack at the callee site, I
> just don't see how s.sizeof can be unknown at the time the function is
> instantiated, /even if/ was declared using a template. What am I
> missing?

Right. That was additional information I lost somewhere. 
That's one thing I've heard bandied around here, that structs are passed by value and classes by reference. It seems too peculiar to be true. 
Anyway, my comment was based on the understanding that the parameter is passed by reference. A reference is just a pointer. You don't need to know anything about what it points to until you dereference it. E.g.

foo(opaque* myvar)
{
    if (this.doIt == true) bar(myvar);
}

Can be compiled without ever knowing anything about the opaque type. 
Obviously if its passed by value you know the size because your call pushed it on the stack.

Bruce.





More information about the Digitalmars-d mailing list