struct and default constructor

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 10 13:25:05 PDT 2014


On Fri, 10 Oct 2014 18:50:29 +0000
dcrepid via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> What I mean with the scoped objects is that you still need to 
> provide a constructor with parameters, yes? I will have to try 
> this out myself, but I've avoided it since some people seem to 
> indicate this is going to be deprecated..
only the syntax `scope auto a = new A();` is deprecated, not the whole
concept. you just have to write it this now:

  import std.typecons : scoped; // yep, it's in a library now
  ...
  class A {
    this () { ... }
    ~this () { ... }
  }
  ...
  {
    // allocate class instance on the stack
    auto a = scoped!A();
    ...
  } // here destructor for 'a' will be called

just be careful and don't let 'a' escape the scope. ;-)

> Basically what I was doing code-wise was creating a 'FixedBuffer' 
> structure, which would have its size passed as a template 
> parameter.  The constructor would do a malloc on it, and all the 
> checks for bounds would only need to rely on that template 
> parameter. Net result is that the cost of having a pretty safe 
> bounds-checked buffer was just the size of one pointer.
it's better to use slices for that. you can create struct like this:

  struct A(size_t asize) {
    private ubyte* mData/* = null*/; // '=null' is the default
    private enum mSize = asize;

    @disable this (this);

    ~this () {
      import core.stdc.stdlib;
      if (mData !is null) free(mData);
    }

    // and allocate memory for data in getter
    @property auto data () {
      import core.stdc.stdlib;
      if (mData is null) {
        import core.exception : onOutOfMemoryError;
        mData = cast(typeof(mData))malloc(mSize);
        if (mData is null) onOutOfMemoryError();
      }
      return mData[0..mSize];
    }

    @property size_t length () const @safe pure nothrow @nogc { return mSize; }
    size_t opDollar () const @safe pure nothrow @nogc { return mSize; }

    ubyte opIndex (size_t ofs) @trusted {
      import core.exception : RangeError;
      if (ofs >= mSize) throw new RangeError();
      return data[ofs];
    }

    ubyte opIndexAssign (ubyte v, size_t ofs) @trusted {
      import core.exception : RangeError;
      if (ofs >= mSize) throw new RangeError();
      return (data[ofs] = v);
    }
  }

but then you will not be able to copy it (or you'll need additional
field to keep "don't free" flag, and with it you can use slice instead,
'cause they both will not fit into the pointer). and you will not be
able to pass it to functions anyway, this will not work:

  void doSomethingOnBuffer (ref A buf);

only this:

  void doSomethingOnBuffer (ref A!1024 buf);

i.e. you'll have to write the size in function args.

> I *sorta* get the whole concept of being able to easily reason 
> and work with structures in D, but why then does it have a 
> destructor and a way to disable a default constructor, along with 
> operator overloads and whatnot.  It seems to me its trying to be 
> object-like and POD-like at the same time which doesn't mesh well.
'cause there are two kinds of structs, actually. one kind is POD, and
another is object-like, with methods, destructors and so on. this may be
confusing a little. ;-) just don't mix 'em.

> As far as the 'scoped' object, I was referring to default 
> construction - is it possible?
you can use classes, as i written above. just don't store such
instance anywhere, 'cause it will be overwritten with another data
after exiting the scope and all hell breaks loose.

> I would very readily accept using objects over structs if they 
> had a guarantee of when they are destroyed, and weren't as risky 
> to pass around as C pointers (i.e. possibility of them being 
> null).
you can pass pointers to structs. pointers can be null. ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20141010/7595fead/attachment.sig>


More information about the Digitalmars-d mailing list