Ref parameter: intended behavior or bug?

Bill Baxter dnewsgroup at billbaxter.com
Tue Aug 21 10:47:11 PDT 2007


Mike Parker wrote:
> I knocked up a sample program to demonstrate reference parameters, then 
> got a result that wasn't at all what I expected:
> 
> ====================================
> 
> import std.stdio;
> 
> struct MyStruct
> {
>     ubyte[] contents;
> }
> 
> class MyClass
> {
>     ubyte[] contents;
> }
> 
> void structDefault(MyStruct ms)
> {
>     writefln("Struct default: %d", ms.sizeof);
> }
> 
> void structRef(ref MyStruct ms)
> {
>     writefln("Struct ref: %d", ms.sizeof);
> }
> 
> void classDefault(MyClass mc)
> {
>     writefln("Class default: %d", mc.sizeof);
> }
> 
> void main()
> {
>     MyStruct ms;
>     MyClass mc = new MyClass;
>     
>     structDefault(ms);
>     structRef(ms);
>     classDefault(mc);
> }
> ==================================
> 
> Here's the output:
> 
> Struct default: 8
> Struct ref: 8
> Class default: 4
> 
> The first and last are what I thought they would be, but I expected the 
> Struct ref line to output 4, since it's supposed to be a ref parameter. 
> I assume this to be a bug, but want to make sure there's not something 
> intended going on.

'ref' is not a type constructor in D, it is a storage class.
.sizeof gives the size of the type.  The storage class shouldn't affect 
that.

--bb


More information about the Digitalmars-d-learn mailing list