What's best practice to use compound literals with structs and C-APIs?
Alex
sascha.orlov at gmail.com
Wed May 1 14:23:37 UTC 2019
On Wednesday, 1 May 2019 at 12:47:22 UTC, Robert M. Münch wrote:
> I use some C library that uses structs and many functions that
> use pointer to structs as arguments:
>
> struct A {...};
> myfunc(A *myA);
>
> In C you can do this to get a lvalue:
>
> myfunc(&(A){...});
>
> In D this doesn't work and I get an "is not an lvalue and
> cannot be modified".
>
> What's the correct D-ish way in such a case?
Look, even in C you have to workaround the case, where you want
to pass a value instead of a pointer. So, the proper question
would be, how to redesign, so that &(A){...} is not needed.
However, to rebuild the same structure, auto ref parameters may
be appropriate.
https://dlang.org/spec/template.html#auto-ref-parameters
or you new A(...) in place
or you use the byRef-pattern as shown in d-idioms
https://p0nce.github.io/d-idioms/#Rvalue-references:-Understanding-auto-ref-and-then-not-using-it
Not sure, which part is under your control...
More information about the Digitalmars-d-learn
mailing list