wlav / CPyCppyy

Other
23 stars 20 forks source link

Improve priority assignment of classes in same inheritance tree #17

Closed guitargeek closed 6 months ago

guitargeek commented 6 months ago

The current implementation of the CPPMethod::GetPriority function considers only direct bases of the class type given as input parameter to a certain function. This can lead to situations which are easily distinguishable but that lead to the same overload being called when passing instances of different classes of the same inheritance tree.

For example,

import ROOT

ROOT.gInterpreter.Declare(
'''
class A {};
class B: public A {};
class C: public B {};

void myfunc(const B &b){
    std::cout << "B" << std::endl;
}

void myfunc(const C &c){
    std::cout << "C" << std::endl;
}

''')

ROOT.myfunc(ROOT.B())
ROOT.myfunc(ROOT.C())

Prints B for both function calls. This commit introduces a new function in cppyy that retrieves the number of bases in the longest branch of the inheritance tree for a certain class. This helps better distinguish cases like the one above, that are now solved.

The new logic still does not solve another issue, namely the fact that in certain situations which would throw errors in C++ due to their ambiguity, cppyy resorts to calling the function overload with the highest priority. A couple of tests have been added in roottest to show both behaviours.