2

I was surprised to find that the following function is not automatically wrapped by SWIG. Surely, I must have done something wrong, as it is a very basic example.

Interface file (stdmap.i):

%module stdmap;

%include "std_map.i"
%template(map_ii) std::map<int, int>;

%{
    extern std::map<int, int> test_map();
%}
extern std::map<int, int> test_map();

Implementation (stdmap.cpp):

#include <map>

std::map<int, int> test_map() {
    return {{1, 0}, {4, 5}};
}

When wrapping to Python, I get

> test_map()
<stdmap.map_ii; proxy of <Swig Object of type 'std::map< int,int > *' at 0x10801dd50> >

and not

{1: 0, 4: 5}

as expected. Where do I make a mistake?

Nibor
  • 1,236
  • 9
  • 23
  • There's an answer (https://stackoverflow.com/a/9041629/9214985) to an earlier SWIG + std::map question that you may find interesting, if you haven't already seen it. This implies that std::map may not wrap beautifully cleanly when passed through a SWIG interface. – rwp Apr 23 '18 at 18:15
  • Yes, but the last comments says: "It appears you don't even need to use the map_string_string type in your Python code. Once you declare the template, SWIG can automatically figure out how to convert a dict to it.". In my code, that's not happening. – Nibor Apr 24 '18 at 06:56

0 Answers0