Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

%template ordering in interface file #275

Open
tmaslach opened this issue Dec 15, 2014 · 2 comments
Open

%template ordering in interface file #275

tmaslach opened this issue Dec 15, 2014 · 2 comments
Labels

Comments

@tmaslach
Copy link

I have been working with %template and std_vector and have been running into some issues. I’m running on windows, but I don’t believe this will be platform specific. I also use the following command line to build (don’t think it’s relevant, but I’m adding it just in case):

swig.exe -threads -python -modern -c++ -Iswig\lib\python -Iswig\lib\std -Iswig\lib mod.i

I am currently using swig 2.0.10, but I believe wsfulton as seen this in 3.X as well (I originally sent a swig-devel email).

Here is my interface file, with C++ code baked in to make things easier:

%module(directors="1", allprotected="1") mod

%{
       template<typename T>
       class myTCouple
       {
       public:
              myTCouple() {}
              static T get5c (const std::vector<myTCouple<T> > &l) { return (T) 5; }
       };
       typedef myTCouple<double> myCouple;
       typedef std::vector<myCouple> myCoupleList;

       double get5  (const myCoupleList &l) { return 5; }
       double get5b (const std::vector<myCouple> &l) { return 5; }
%}

%include "std_vector.i"

template<typename T>
class myTCouple
{
public:
       myTCouple();
       static T get5c (const std::vector<myTCouple<T> > &l);
};

typedef myTCouple<double> myCouple;
typedef std::vector<myCouple> myCoupleList;

%template (myCouple) myTCouple < double >;
%template (myCoupleList) std::vector<myCouple>;

double get5 (const myCoupleList &l);
double get5b (const std::vector<myCouple> &l);

And, last of all, here is the python code I’m trying to execute:

from mod import *
lst = [myCouple(), myCouple()]
get5(lst)           # Should print 5
get5b(lst)          # Should print 5
myCouple.get5c(lst) # Should print 5

get5, get5b work. But myCouple.get5c does not..

Here is a slightly modified version (for clarity, getting rid of other answers not relevant to this issue) of William's response (swig-devel, emailed 12/15/2014 2:14AM EST):

However, there is a catch 22 here as myTCouple is used within the myTCouple class itself. SWIG's handling of this is buggy and I suggest you raise a bug report in Github for this case. If you swap the two %template calls around [...], it will work. This is the only workaround I could find and is technically incorrect as the two %template calls are then the wrong way round.

@ojwb
Copy link
Member

ojwb commented Jan 27, 2022

Still reproducible with current git master (and swapping the order still makes it work).

@ojwb ojwb added the C++ label Jan 27, 2022
@ojwb
Copy link
Member

ojwb commented May 19, 2023

There have been quite a few %template-related fixes since Jan 2022 so I retested, but this is still reproducible with current git master (and swapping the order still makes it work).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants