bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.46k stars 581 forks source link

Fix template substitution with const #677

Closed HGuillemet closed 1 year ago

HGuillemet commented 1 year ago
template <typename T>
class X {
 public:
    X(const T*) {}
};

If you instantiate this template with int *, then the resulting type should be int * const * and not const int **.
But the parser produces this, with a new Info("X<int *>").pointerTypes("XI"):

    public XI(@Cast("const int**") PointerPointer arg0) { super((Pointer)null); allocate(arg0); }
    private native void allocate(@Cast("const int**") PointerPointer arg0);

Leading to JNI compile error since the constructor expects a int * const*. This PR tries to fix this. It seems to do the trick. The parser now emits:

    public XI(@Cast("int**") PointerPointer arg0) { super((Pointer)null); allocate(arg0); }
    private native void allocate(@Cast("int**") PointerPointer arg0);

The const doesn't appear in the cast at all, but at least we have a better chance to compile the JNI. I'm not sure it generalizes well. Can we do something better ?

saudet commented 1 year ago

Info.constPointer currently gets ignored in this case, yes. It's complicated, but if this fix works for that template that's fine for now. We can do something about Info.constPointer later, I think that's OK, yes. Did you make sure it doesn't break anything in the presets though?

HGuillemet commented 1 year ago

Just finished to reparse (almost) all presets. Only changes are for pytorch where const is removed from @Cast in the following declarations:

In BlockArrayRef.java:

public native @Cast("torch::jit::Block**") PointerPointer data();

In ValueArrayRef.java

public native @Cast("torch::jit::Value**") PointerPointer data();

JNI still compiles.