C++ templates in PyRoot

Hi there,

I was wondering, how/if PyRoot can handle templated classes from C++? I know that SWIG can do it by providing a special control sequence which in the end perhaps creates an specific instance of the templated class.
I couldn’t find any information on the matter for PyRoot though. Maybe I oversaw it (Sorry in that case!). Perhaps the LinkDef file is a valid ansatz to handle templated classes.

I want to make sure that it can/cannot handle them before I sit down and write some code.

Cheers,

Peter

Hi Peter,

[quote=“psteinb”]I was wondering, how/if PyRoot can handle templated classes from C++? I know that SWIG can do it by providing a special control sequence which in the end perhaps creates an specific instance of the templated class.
I couldn’t find any information on the matter for PyRoot though. Maybe I oversaw it (Sorry in that case!). Perhaps the LinkDef file is a valid ansatz to handle templated classes.[/quote]

It’s not obvious to me (and it may just be me) what you mean. Yes, PyRoot can be used to access a MyFancyTemplate< SomeObject > as long as there is a proper dictionary loaded. If that’s not what you’re asking, then try again. :smiley:

Cheers,
Charles

Peter,

yes, what Charles said, and with the addition that the syntax on the python side is “templatename( targ1, targ2, … )( carg1, carg2, … )” where “targN” are the template arguments (either one string to represent them all, individually, or actual classes) and “cargN” are the arguments to the constructor.

In retrospect, it probably should have been [] rather than () for the template instantiation, as that would have led to somewhat clearer syntax, but this is what it is.

Only corner case are templated member functions of classes. They do work, but can be added later to a class by spreading them out over various different dictionaries, which spells trouble (basically, the overloads then change half-way during the program; not something you want to rely on). C++ suffers, in a way, from a similar problem, though.

Cheers,
Wim

Dear Charles and Wim,

thanks for your replies. I understood that if I supply the correct dictionary (created by a LinkDef file steered rootcint call) that contains all permutations of possible template parameters, I can use C++ templated classes from within Python.

I had a project in mind that uses multiple template parameters a la Design-by-Policy to be accesible in python. But OK, I guess I’ll opt for dynamic change of behaviour via inheritance then to allow the packages’s use through PyRoot.

Thanks for PyRoot - I think it is a valuable addition to the ROOT family.

Keep it up,

Peter