Hi,
I am trying to use template class in c++ which I would like to use from python. A simple example here:
#include <iostream>
using namespace std;
template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second);
};
template <class T>
mypair<T>::mypair(T first, T second){a=first; b=second;}
I can create an object for this class in c++ as: mypair <int>myobject (100, 75);
My question is how to create myobject like instance in python?
Normally I create an object in python like below: (supposing above code is saved in cppcode.cc file)
~ python
Python 3.8.3 (default, May 17 2020, 18:15:42)
[GCC 10.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>> ROOT.gInterpreter.Declare("""template <typename T> T foo(T t) { return t*2; }""")
True
>>> ROOT.foo(4)
8
>>> ROOT.foo(4.)
8.0
>>> ROOT.foo("x")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Template method resolution failed:
none of the 2 overloaded methods succeeded. Full details:
int ::foo(int t) =>
TypeError: could not convert argument 1 (int/long conversion expects an integer object)
double ::foo(double t) =>
TypeError: could not convert argument 1 (must be real number, not str)
Failed to instantiate "foo(std::string)"
For your usecase:
~ python
Python 3.8.3 (default, May 17 2020, 18:15:42)
[GCC 10.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>> ROOT.gInterpreter.Declare("""template <class T>
... class mypair {
... T a, b;
... public:
... mypair (T first, T second);
... };
... template <class T>
... mypair<T>::mypair(T first, T second){a=first; b=second;}
... """)
True
>>> ROOT.mypair('int')(1,2)
<cppyy.gbl.mypair<int> object at 0x560cb99f2150>