0

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.

Carsten
  • 1,912
  • 1
  • 28
  • 55
  • 1
    Do you have another comparePairs function with different input parameters? If yes, try to wrap the function with a lambda like in this example: https://godbolt.org/z/YLrGAb – CuriouslyRecurringThoughts Aug 03 '19 at 14:26
  • It does compile in C++ because both `comparePairs` functions are declared in the same file in which they are called respectively. This is about FastText, see [here](https://github.com/facebookresearch/fastText/blob/40a77442a756ab160ae3465b26322f6e480405d9/src/fasttext.cc#L815) and [here](https://github.com/facebookresearch/fastText/blob/40a77442a756ab160ae3465b26322f6e480405d9/src/loss.cc#L20). – Carsten Aug 03 '19 at 15:07
  • Then must be something related to JavaCpp, and I know nothing about it. Sorry, I hope somebody else can help you. – CuriouslyRecurringThoughts Aug 03 '19 at 15:29
  • If this can be worked around with a small patch, I would say that's fine. We can bundle this easily, no need to modify anything upstream. – Samuel Audet Aug 19 '19 at 10:22

1 Answers1

0

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

Carsten
  • 1,912
  • 1
  • 28
  • 55