[C++-sig] how to disambiguate 2 overloaded mem funs each with just 1 arg

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[C++-sig] how to disambiguate 2 overloaded mem funs each with just 1 arg

DRandallTaylor
Hello,
N00bie here.
How do I disambiguate 2 member functions each having just 1 arg:

struct C
{
   int len(const char* const val) { return ::strlen(val); }
   int len(const std::string &val) { return val.size(); }
};
BOOST_PYTHON_MODULE(pycore)
{
  boost::python::class_<C>("C")
   .def("len"
        , &C::len
        , boost::python::return_value_policy< python::return_by_value >()
   );
}

There must be some way to tell boost::python that when I pass a python str
object to C::len then convert the arg to a "const char* const" and call the
first overload of C::len.  (I told you this was going to be easy! But not for
me because I'm n00b.).

Cheers,
DRandallTaylor








_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Reply | Threaded
Open this post in threaded view
|

Re: [C++-sig] how to disambiguate 2 overloaded mem funs each with just 1 arg

David Abrahams
DRandallTaylor <[hidden email]> writes:

> Hello,
> N00bie here.
> How do I disambiguate 2 member functions each having just 1 arg:
>
> struct C
> {
>    int len(const char* const val) { return ::strlen(val); }
>    int len(const std::string &val) { return val.size(); }
> };
> BOOST_PYTHON_MODULE(pycore)
> {
>   boost::python::class_<C>("C")
>    .def("len"
>         , &C::len
>         , boost::python::return_value_policy< python::return_by_value >()
>    );
> }
>
> There must be some way to tell boost::python that when I pass a python str
> object to C::len then convert the arg to a "const char* const" and call the
> first overload of C::len.  (I told you this was going to be easy! But not for
> me because I'm n00b.).


  boost::python::class_<C>("C")
   .def("len"
        , (int (C::*)(char const*))&C::len
        , boost::python::return_value_policy< python::return_by_value >()
   );

HTH,
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig