wrapping specialised c++ template class with swig

后端 未结 1 1455
借酒劲吻你
借酒劲吻你 2021-01-03 01:16

consider the following class declarations:

namespace X {
template
class Foo {
public:
    Foo();
    virtual ~Foo();

    T value() const;
};
         


        
相关标签:
1条回答
  • 2021-01-03 02:14

    You can achieve the result you want by doing:

    %include "stl.i"
    %include "std_string.i"
    %include "std_vector.i"
    
    namespace X {
    using namespace std;
    
    %rename(FooVectorInt) Foo<std::vector<int> >;
    
    class Foo<std::vector<int> > {
    public:
        virtual ~Foo();
        int value( const int ) const;
    };
    
    template<class T>
    class Foo {
    public:
        Foo();
        virtual ~Foo();
        T value() const;
    };
    
    %template(FooInt) Foo<int>;
    %template(FooString) Foo<string>;
    }
    

    This works because what you write in the interface file isn't C++ and all that matters is that the correct code is generated by SWIG. If you want to repeat this lots you can write macros (which is close to what %template is anyway).

    Still this isn't a very clean solution - I expected this to "just work" with the specialisations and I can't see a simpler workaround either.

    0 讨论(0)
提交回复
热议问题