test_overload_resolution.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright Troy D. Straszheim 2009
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // example that shows problems with overloading and automatic conversion.
  8. // if you call one of the below functions from python with bool/int/double,
  9. // you'll see that the overload called is first match, not best match.
  10. // See overload matching in luabind for an example of how to do this better.
  11. //
  12. // see this mail:
  13. // http://mail.python.org/pipermail/cplusplus-sig/2009-March/014362.html
  14. //
  15. // This test isn't called by the cmake/jamfiles. For future use.
  16. //
  17. #include <boost/python/module.hpp>
  18. #include <boost/python/def.hpp>
  19. #include <complex>
  20. #include <boost/python/handle.hpp>
  21. #include <boost/python/cast.hpp>
  22. #include <boost/python/object.hpp>
  23. #include <boost/python/detail/wrap_python.hpp>
  24. using boost::python::def;
  25. using boost::python::handle;
  26. using boost::python::object;
  27. using boost::python::borrowed;
  28. std::string takes_bool(bool b) { return "bool"; }
  29. std::string takes_int(int b) { return "int"; }
  30. std::string takes_double(double b) { return "double"; }
  31. BOOST_PYTHON_MODULE(overload_resolution)
  32. {
  33. def("bid", takes_bool);
  34. def("bid", takes_int);
  35. def("bid", takes_double);
  36. def("dib", takes_double);
  37. def("dib", takes_int);
  38. def("dib", takes_bool);
  39. def("idb", takes_int);
  40. def("idb", takes_double);
  41. def("idb", takes_bool);
  42. def("bdi", takes_bool);
  43. def("bdi", takes_double);
  44. def("bdi", takes_int);
  45. }