C++ SWIG:包装std::map

C++ SWIG:包装std::map,c++,swig,C++,Swig,对于值是指针而不是键的映射,我发现了一个类似的问题。我遇到以下编译器错误: error: no member named 'type_name' in 'swig::traits<C>' 当我写我自己的类型映射或使用SWIG std_映射时都会发生这种情况。我需要采取哪些额外步骤来为指向的类型提供类型名称 最简单的工作示例: %module stdmap; %include "std_map.i" %{ class C { public:

对于值是指针而不是键的映射,我发现了一个类似的问题。我遇到以下编译器错误:

error: no member named 'type_name' in 'swig::traits<C>'
当我写我自己的类型映射或使用SWIG std_映射时都会发生这种情况。我需要采取哪些额外步骤来为指向的类型提供类型名称

最简单的工作示例:

%module stdmap;

%include "std_map.i"

%{
    class C
    {
        public:
             C() {};
    };
%}

class C
{
    public:
        C();
};

%template(mymap) std::map<int, C*>;

SWIG可能对类指针感到困惑,因为它的包装器无论如何都使用指针。在任何情况下,SWIG文件都会说粗体地雷:

本节中的库模块提供对包括STL的标准C++库的部分的访问。SWIG对STL的支持是一项持续的工作。对某些语言模块的支持相当全面,但一些使用较少的模块没有编写足够多的库代码

如果您可以随意更改实现,我认为有两种变通方法可以起作用。我使用Python作为测试的目标语言:

使用std::map: 使用std::map:
我确实需要一个指针,因为我需要映射中的运行时多态性。我可以试试你的建议。
%module stdmap

%include "std_map.i"

%inline %{

#include <memory>

class C
{
public:
    C() {};
};

%}

%template(mymap) std::map<int, C>;
>>> import stdmap
>>> c = stdmap.C()
>>> m = stdmap.mymap()
>>> m[1] = c
>>> c
<stdmap.C; proxy of <Swig Object of type 'C *' at 0x00000263B8DA5780> >
%module stdmap

%include "std_map.i"
%include "std_shared_ptr.i"
%shared_ptr(C)

%inline %{

#include <memory>

class C
{
public:
    C() {};
};

%}

%template(mymap) std::map<int, std::shared_ptr<C> >;
>>> import stdmap
>>> c = stdmap.C()
>>> m = stdmap.mymap()
>>> m[1] = c
>>> c
<stdmap.C; proxy of <Swig Object of type 'std::shared_ptr< C > *' at 0x00000209C44D5060> >