我遇到了一个SWIG生成的C++类包装器的奇怪问题,其中我似乎无法使用std::map包装为std::shared_ptr类型的标准访问器函数.我设法生产了一个MWE,它重现了我观察到的奇怪行为.
TestMap.h
#include <iostream>
#include <map>
#include <memory>
class fooType{
public:
fooType() { };
~fooType() { };
void printFoo() { std::cerr << "FOO!" << std::endl; }
static std::shared_ptr<fooType> make_shared() {
return std::shared_ptr<fooType>(new fooType());
}
};
class testMap : public std::map<int, std::shared_ptr<fooType> > {
public:
void printBar() { std::cerr << "bar." << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)
然后是我的SWIG界面文件:
TestMap.i
%module TestMap
%include <std_map.i>
%include <std_shared_ptr.i>
%{
#include "TestMap.h"
%}
%shared_ptr(fooType);
%shared_ptr(testMap);
%shared_ptr(std::map<int, std::shared_ptr<fooType> > );
%template(fooMap) std::map< int, std::shared_ptr<fooType> …Run Code Online (Sandbox Code Playgroud)