Using a struct as a wrapper for an extern(C) opaque type, no default constructor, what do?

Jakob Ovrum via Digitalmars-d-learn digitalmars-d-learn at
Wed Dec 16 19:43:58 PST 2015

On Thursday, 17 December 2015 at 03:31:37 UTC, Jeremy DeHaan 
> Hi all. I'm interfacing to some C code which include an opaque 
> type and some C functions that create and work with a pointer 
> to that type. I want to wrap up everything in a struct, and the 
> only thing that seems to bug me is initialization.
> Since it is C code, I obviously can't read the function that 
> creates the opaque type. Not only that, I can't define a 
> default constructor. What are my options here?
> This is for an API that is intended to be used by people other 
> than myself, so I'd like to use something that doesn't look 
> ugly or isn't a hack. I really don't like the idea of using a 
> factory method or overriding opCall. Am I basically out of luck 
> and need to resort to one of these methods?

Using a factory function alongside @disable this(); is the 
canonical way to do this. Although, if your struct is a reference 
type, you can simply allow default construction and have it mean 
a null reference.

Using static opCall here is just a factory function with special 
syntax, but I think it does more harm than good.

More information about the Digitalmars-d-learn mailing list