[C++-sig] Implementation of proper overload resolution

Troy D. Straszheim troy at resophonic.com
Thu Dec 17 23:54:58 CET 2009


Neal Becker <ndbecker2 at gmail.com> writes:

> I assume overload resolution extends to scoring multiple arguments as well?
>

Sure.  This is the reason that scores are optional<unsigned>.  If any
single argument scorer returns optional<unsigned>() (meaning 'unsuitable'),
this stops evaluation and kills the score for the entire signature.
Here are some excerpts from the test suites:

//------------------------------------------------------

std::string f1(float, bool)         { return "float,bool";        }
std::string f2(float, int)          { return "float,int";         }
std::string f3(float, std::string)  { return "float,std::string"; }

BOOST_PYTHON_MODULE(ambig6_ext)
{
  def("f", &f1);
  def("f", &f2);
  def("f", &f3);
}

//------------------------------------------------------

>>> from ambig6_ext import f
>>> help(f)
Help on built-in function f:

f(...)
    f( (float)arg1, (bool)arg2) -> str :
    
        C++ signature :
            std::string f(float,bool)
    
    f( (float)arg1, (int)arg2) -> str :
    
        C++ signature :
            std::string f(float,int)
    
    f( (float)arg1, (str)arg2) -> str :
    
        C++ signature :
            std::string f(float,std::string)

>>> f(1.0, True)    # perfect match, score 0
'float,bool'
>>> f(1.0, 1)       # perfect match, score 0 
'float,int'
>>> f(True, True)   # best match arg1 score is 1, arg2 score is 0
'float,bool'


#
# Here, note that f3 is not listed in the set of ambiguous candidates,
# as the arg2 score (conversion of float to string) is 'unsuitable' and
# removes it from consideration
#
>>> f(1.0,1.0)      # ambiguous, f1 & f2 both score 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.AmbiguousCall: Ambiguous call to Boost.Python function ambig6_ext.f
C++ signatures:
    f(float, int)
    f(float, bool)


#
#  If the second arg is a string, then the 3rd overload
#  always matches...
#
>>> f(True, 'helloverloading')   # best match, score 1
'float,std::string'
>>> f(1, 'helloverloading')      # best match, score 1
'float,std::string'
>>> f(1.0, 'helloverloading')    # perfect match, score 0
'float,std::string'


#
# ... Unless the arg1 is also a string.  Note this one is ArgumentError,
#  listing all overloads, not AmbiguousCall listing the ambig. ones.
#
>>> f('uh', 'oh')                # all overloads score 'unsuitable'
Boost.Python.ArgumentError: Python argument types in
    ambig6_ext.f(str, str)
did not match C++ signature:
    f(float, std::string)
    f(float, int)
    f(float, bool)


-t


More information about the Cplusplus-sig mailing list