bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.43k stars 576 forks source link

std::unique_ptr fields #717

Closed HGuillemet closed 9 months ago

HGuillemet commented 9 months ago
#include <memory>

struct S {
  std::unique_ptr<int> p;
};

parses to this getter and setter:

  public native @UniquePtr IntPointer p(); public native S p(IntPointer setter);

Compiling the JNI triggers:

error: ambiguous overload for 'operator=' (operand types are 'std::unique_ptr<int>' and 'UniquePtrAdapter<int>')
 1178 |     ptr->p = adapter0;

= for unique_ptr has 2 overloads : one taking a std::unique_ptr && and another taking a const std::unique_ptr &. And the adapter has cast overloads for both.

What's the best way to handle this ?

(case seen in Pytorch 2.1)

saudet commented 9 months ago

I guess either one work, but give it a try with a @Cast like there already are for SharedPtrAdapter.

HGuillemet commented 9 months ago

i don't think @Cast works on fields setter.

saudet commented 9 months ago

I'm sure it does, try with the third element.

HGuillemet commented 9 months ago

Ok, it seems to work with this info:

new Info("S::p").annotations("@UniquePtr", "@Cast({\"\", \"\", \"std::unique_ptr<int>&&\"})")

The @Cast is put on the getter only, but it affects the generation of the setter JNI, somehow.

Thanks, closing this issue.