【问题标题】:wrapping specialised c++ template class with swig用 swig 包装专门的 c++ 模板类
【发布时间】:2012-03-18 10:39:48
【问题描述】:

考虑以下类声明:

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

    T value() const;
};

template<class T>
class Foo<vector<T>> {
public:
    Foo();
    virtual ~Foo();

    T value( const int ) const;
};
}

对于他们,我在 foo.i 文件中有以下声明

%include "stl.i"
%include "std_string.i"
%include "std_vector.i"

namespace X {
using namespace std;

template<class T>
class Foo {
public:
    Foo();
    virtual ~Foo();
    T value() const;
};

template<class T>
class Foo<vector<T> > {
public:
    Foo();
    virtual ~Foo();
    T value( const int ) const;
};

%template(FooInt) Foo<int>;
%template(FooString) Foo<string>;
%template(FooVectorInt) Foo<vector<int> >;

}

这两个类之间的区别在于后者对向量容器的特殊化 value() 方法的不同签名,其中第一个不带参数,而第二个需要 an整数。

swig 整理的包装器代码包装了%template(FooVectorInt) 错误,因为它调用了value() 方法而不是专用向量方法value(const int)。给我以下编译错误消息:

foo_wrap.cxx: in function »int _wrap_FooVectorInt_value(lua_State*)«:

/home/noobsaibot/foo/bindings/src/lua/foo_wrap.cxx:6706:81: error: no matching function to call »X::Foo<std::vector<int> >::value() const«
/home/noobsaibot/foo/src/foo.h:78:5: note: candidate is: T X::Foo<std::vector<_RealType> >::value(int) const [with T = int, int = unsigned int]

关于我可能缺少什么以使 swig 了解哪个功能是正确的功能的任何建议?

干杯

【问题讨论】:

    标签: c++ templates swig


    【解决方案1】:

    你可以通过以下方式达到你想要的结果:

    %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>;
    }
    

    这是可行的,因为您在接口文件中编写的内容不是 C++,重要的是 SWIG 生成了正确的代码。如果你想重复这些,你可以编写宏(无论如何都接近%template)。

    这仍然不是一个非常干净的解决方案 - 我希望这对专业化“有效”,我也看不到更简单的解决方法。

    【讨论】:

    • 忘记我写的,我没有收到错误。现在开始检查它是​​否做了它应该做的事情:) ...谢谢到目前为止。
    • 它编译并且我得到了“正确的”对象 Foo>,但是,lua 不知道该类型 ... 使对象在 lua 中不可用:/ ...
    • @noobsaibot - “lua 不知道”是什么意思?看起来它已在我所做的 Python 测试中正确注册。 (我这辈子没用过lua)
    • 没关系,我已经重构了我的代码,因此基于集合的类的名称与简单类的名称不同。问题解决了:)
    猜你喜欢
    • 2012-01-21
    • 2011-09-07
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多