[C++-sig] Exposing a generic template system in Boost.Python

Stefan Seefeld stefan at seefeld.name
Wed Mar 19 18:36:27 CET 2014


Francesco,

I have done something like what you are suggesting. It essentially boils
down to defining a function template like

template <typename T>
void define_vector(char const *name)
{
   class_<...> vector(name);
  ...
}


and then calling that multiple times:

  define_vector<int>("IVector");
  define_vector<long>("LVector");
  ...

Providing a factory function that instantiates one of those based on a
value-type selector as you want can be easily done on the Python side.

I don't think this can be automated any further. In particular, it is
clear that any type you may want to instantiate in Python has to be
compiled explicitly into the extension module. I.e., you need to
explicitly instantiate the templates above, by explicitly calling the
functions for the types you want to see supported at runtime. There is
no JIT compilation for this.

    Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list