Opaque structs

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Jun 28 13:16:33 PDT 2013


On 6/28/13, Johannes Pfau <nospam at example.com> wrote:
> A naive question: Why isn't struct S {} enough? This should be a struct
> with size 0 so why do we need to disable the constructor and postblit
> explicitly?

Because the user should never be able to use such a struct by value,
in other words a user might mistakenly write code such as:

-----
struct S { }

extern(C) S* get();
extern(C) void call(S*);

void main()
{
    S* s = getS();

    S s2 = *s;  // copies 1 byte
    call(&s2);  // no telling what will happen on the C side, usually
memory corruption + crash
}
-----


More information about the Digitalmars-d-learn mailing list