[C++-sig] exposing Template methods and classes

Albert Strasheim fullung at gmail.com
Fri May 25 18:27:47 CEST 2007


Hello

----- Original Message ----- 
From: "Sean Ross-Ross" <srossross at gmail.com>
To: "Development of Python/C++ integration" <c++-sig at python.org>
Sent: Friday, May 25, 2007 5:18 PM
Subject: [C++-sig] exposing Template methods and classes


> Is there a way that I can smartly template classes or functions
> depending on the type of python Object passed to them?
>
> for example, say you expose a vector class and as a constructor pass
> a extra argument "dtype", like numpy, that templates the vector to
> that  type.
>
> or any what at all that does not involve templating it on a PyObject.
> So I don't have to template every type that I could possibly want?

You're going to have to template on every type you possibly want, or the 
code that has to deal with that type isn't going to be in your extension 
module.

My code is still a bit messy, but I'm playing with the idea of using 
Boost.Python's def_visitor to make this a bit easier. Take a look here:

http://pyspkrec.googlecode.com/svn/numpycpp/src/multi_array_test.cpp
http://pyspkrec.googlecode.com/svn/numpycpp/src/ublas_matrix_test.cpp

I have a struct called generic_visitor which basically contains all the 
repetitive type stuff, and then when I wrap a specific method, I write 
something much smaller (a copy_visitor in those files) to wrap a specific 
method. Then the def part only says: def(generic_visitor<copy_visitor>()).

Hope this helps.

Cheers,

Albert 




More information about the Cplusplus-sig mailing list