What's best practice to use compound literals with structs and C-APIs?
    Robert M. Münch 
    robert.muench at saphirion.com
       
    Wed May  1 14:59:48 UTC 2019
    
    
  
On 2019-05-01 14:23:37 +0000, Alex said:
> 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.
That's not a workaround in C, it's the way to do it.
> However, to rebuild the same structure, auto ref parameters may be appropriate.
> https://dlang.org/spec/template.html#auto-ref-parameters
That would need me to change myfunc which is not possible because the D 
binding is all generated from C code.
> or you new A(...) in place
Doesn't work because this seems to kick in some D releated run-time 
stuff which lead to unresolved externals during linking:
error LNK2001: Nicht aufgelöstes externes Symbol "...__initZ".
error LNK2001: Nicht aufgelöstes externes Symbol 
"...__xtoHashFNbNeKxSQBtQBoQBfZm".
error LNK2001: Nicht aufgelöstes externes Symbol 
"...__xopEqualsFKxSQBsQBnQBeKxQmZb".
Not sure, why this happens.
> 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 
> 
This again would need my to change the C bindings, which are generated.
-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
    
    
More information about the Digitalmars-d-learn
mailing list