[C++-sig] staticmethod usage

Milind Patil milind_patil at hotmail.com
Tue Apr 8 02:54:51 CEST 2003


I have only recently started experimenting with boost.c++ package and pyste.
to the developers of these excellent packages.

Along the way I faced some issues. Some have been persistent and I am kinda
Please look at the code(hello.cpp) below to illustrate some of the issues...

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python.hpp>

using namespace boost::python;

class Y {
    Y() : y(0) { }
    Y(int y) : y(y) { }
    Y(Y const& rhs) : y(rhs.y) { }
    virtual ~Y() { }

    operator int() const { return y; }

    void operator=(Y const& y) {
        this->y = y.y;

    int y;

class X {
    X(): a(0), b(0) { }
    X(Y a, Y b) : a(a), b(b) { }
    X(Y a, Y b, void c (Y *)) : a(a), b(b) { }

    void show() {std::cout << " show " << endl; }
    static void show(const char * s) {std::cout << "static show " << s <<
endl; }
    static void tryitout (const char* s, Y y, Y z = NULL) {std::cout <<
"static tryitout " << s << endl; }
    Y a;
    Y b;

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_tryitout_overloads_2_3, tryitout,
2, 3)

    class_<Y> ("Y");

    class_<X> ("X")
        .def_readwrite("a", &X::a)
        .def_readwrite("b", &X::b)
        //.def("tryitout", (void (*)(const char *, Y, Y =
NULL ) )&X::tryitout, X_tryitout_overloads_2_3())
        .def("tryitout", (void (*)(const char *, Y, Y =
NULL ) )&X::tryitout )
        .def ("show", (void (X::*)() )&X::show)
        .def ("show", (void (*)(const char *) )&X::show)

    implicitly_convertible<int, Y>();
    implicitly_convertible<Y, int>();

Essentially I have an overloaded function "show:, one of them been static.
The other staic function is
"tryitout" with optional argument. I am trying to use them thus:

import hello

x = hello.X()
y = hello.Y()

print hello.X.show("X.show")

print x.tryitout("x.tryitout", y, y)

print x.tryitout("sd", y)

print x.show()

My  issues (with boost_1_30_0, gcc 2.95 and python2.2.2):
a) If I uncomment
//.def("tryitout", (void (*)(const char *, Y, Y ) )&X::tryitout,
the compilation fails. The compiler message is long and unintelligible (to

How do I use BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS with static methods?

b) If I use
.def("tryitout", (void (X::*)(const char *, Y, Y  ) )&X::tryitout )

which is what pyste generated, I get compilation problems.
The compiler cannot find the function (void (X::*)(const char *, Y,
Y ) )&X::tryitout .

Why does def("tryitout", (void (*)(const char *, Y, Y  ) )&X::tryitout )
work and not
the above?

c) The python code gives exception at
print x.show()
saying args are not right.

How do I expose overloaded functions one of which is static? Note that the
arguments to
the overloaded functions are different.

Any help is appreciated.

Thank you,

More information about the Cplusplus-sig mailing list