javac++javacpp

JavaCPP fails at function disambiguation


I am implementing a Java wrapper for a C++ project using JavaCPP. I have defined mappings for all custom types, but I'm struggling with a call to the std::sort_heap() function that takes a function as an argument.

This is the function call in the C++ code:

std::sort_heap(heap.begin(), heap.end(), comparePairs);

comparePairs is declared like this in this file:

bool comparePairs(
  const std::pair<real, int32_t>& l,
  const std::pair<real, int32_t>& r) {
    return l.first > r.first;
}

When I try to run this with JavaCPP (using the JavaCPP Maven plugin), I get this error:

error: no matching function for call to ‘pop_heap(std::vector<std::pair<float, int> >::iterator, std::vector<std::pair<float, int> >::iterator, <unresolved overloaded function type>)’
   std::pop_heap(heap.begin(), heap.end(), comparePairs);
                                                       ^

Update: the problem seems to be due to the fact that another compairePairs() with a slightly different signature function is declared in another file. The C++ compiler disambiguates them fine, considering that each compairePairs() function is called only within the same file. However, JavaCPP seems to fail at that disambiguation somehow.

This is how the other type mappings are declared:

import org.bytedeco.javacpp.tools.Info;
import org.bytedeco.javacpp.tools.InfoMap;
import org.bytedeco.javacpp.tools.InfoMapper;
[...]

@Properties(value = @Platform(include = {[...]}), target = "[...]")
public class Wrapper implements InfoMapper {
  public void map(InfoMap infoMap) {
    infoMap.put(new Info("std::vector<std::string>").pointerTypes("StringVector").define())
       // more put calls here
       ;
  }
}

Question is thus: why does JavaCPP fail to disambiguate overloaded functions, and how can I fix it?

Note: the C++ code is a third party project, so changing the C++ code is not an option.

Update: the issues appears to be due to the compairPairs() function being declared twice in the C++ code (in two different files), with different signatures.


Solution

  • As described in the comment, wrapping the function call to comparePairs() into a lambda function resolves the issue.

    See https://github.com/facebookresearch/fastText/pull/936/commits/cda295f1b5851df0a26a6ac2ab04230fb864a89d