#1363 Wrapped erase function incorrectly named for a wrapped std::map type

None
closed
None
5
2014-03-07
2014-03-04
No

With the update to swig 2.0.12, swig now generates invalid C++ code for a template class having a std::map as template argument. Swig generates invalid C++ for the following C++/swig code:

=== wrappedmap.i ===
%module wrappedmap

%include <stl.i>

template<typename T="">
class Wrap {
public:
T* operator->(void) const;
};

%{
template<typename T="">
class Wrap {
public:
T* operator->(void) const { return NULL; }
};
%}

%template(StringDoubleMap) std::map<std::string, double="">;
%template(WrappedMap) Wrap< std::map<std::string, double=""> >;
======

$ swig -version

SWIG Version 2.0.12

Compiled with g++ [i686-pc-linux-gnu]

Configured options: +pcre

Please see http://www.swig.org for reporting bugs and further information
$ swig -c++ -python -o wrappedmap.cpp wrappedmap.i && g++ -fPIC -c wrappedmap.cpp -I/usr/include/python2.7
wrappedmap.cpp: In function 'PyObject _wrap_WrappedMap_eraseSWIG_1(PyObject, PyObject)':
wrappedmap.cpp:8108:293: error: 'std_map_Sl_std_string_Sc_double_Sc_std_less_Sl_std_string_Sg
Sc_std_allocator_Sl_std_pair_Sl_std_string_SS_const_Sc_double_SgSgSgeraseSWIG_1' was not declared in this scope
wrappedmap.cpp: In function 'PyObject
_wrap_WrappedMap_eraseSWIG_2(PyObject, PyObject)':
wrappedmap.cpp:8159:298: error: 'std_map_Sl_std_string_Sc_double_Sc_std_less_Sl_std_string_Sg
Sc_std_allocator_Sl_std_pair_Sl_std_string_SS_const_Sc_double_SgSgSgeraseSWIG_2' was not declared in this scope

Grepping on the swig generated code reveals the following:
$ grep -n std_map_Sl_std_string_._Sgerase wrappedmap.cpp
5172:SWIGINTERN void std_map_Sl_std_string_Sc_double_Sg
eraseSWIG_1(std::map< std::string,double > *self,std::map< std::string,double >::iterator position){ self->erase(position); }
5173:SWIGINTERN void std_map_Sl_std_string_Sc_double_Sg
eraseSWIG_2(std::map< std::string,double > *self,std::map< std::string,double >::iterator first,std::map< std::string,double >::iterator last){ self->erase(first, last); }
6974: std_map_Sl_std_string_Sc_double_Sg
eraseSWIG_1(arg1,arg2);
7025: std_map_Sl_std_string_Sc_double_Sg
eraseSWIG_2(arg1,arg2,arg3);
8108: std_map_Sl_std_string_Sc_double_Sc_std_less_Sl_std_string_Sg
Sc_std_allocator_Sl_std_pair_Sl_std_string_SS_const_Sc_double_SgSgSgeraseSWIG_1((std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > >
)(arg1)->operator ->(),arg2);
8159: std_map_Sl_std_string_Sc_double_Sc_std_less_Sl_std_string_SgSc_std_allocator_Sl_std_pair_Sl_std_string_SS_const_Sc_double_SgSgSgerase__SWIG_2((std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > >*)(arg1)->operator ->(),arg2,arg3);

This is defect is not present in swig 2.0.11:
$ swig -version

SWIG Version 2.0.11

Compiled with g++ [i686-pc-linux-gnu]

Configured options: +pcre

Please see http://www.swig.org for reporting bugs and further information
$ swig -c++ -python -o wrappedmap.cpp wrappedmap.i && g++ -fPIC -c wrappedmap.cpp -I/usr/include/python2.7
$ ls
wrappedmap.cpp wrappedmap.i wrappedmap.o wrappedmap.py

Discussion

  • Martin Skou Andersen

    Here is the compiler error and the grep output without formatting:
    $ swig -c++ -python -o wrappedmap.cpp wrappedmap.i && g++ -fPIC -c wrappedmap.cpp -I/usr/include/python2.7
    wrappedmap.cpp: In function 'PyObject* _wrap_WrappedMap_erase__SWIG_1(PyObject*, PyObject*)':
    wrappedmap.cpp:8108:293: error: 'std_map_Sl_std_string_Sc_double_Sc_std_less_Sl_std_string_Sg__Sc_std_allocator_Sl_std_pair_Sl_std_string_SS_const_Sc_double_Sg__Sg__Sg__erase__SWIG_1' was not declared in this scope
    wrappedmap.cpp: In function 'PyObject* _wrap_WrappedMap_erase__SWIG_2(PyObject*, PyObject*)':
    wrappedmap.cpp:8159:298: error: 'std_map_Sl_std_string_Sc_double_Sc_std_less_Sl_std_string_Sg__Sc_std_allocator_Sl_std_pair_Sl_std_string_SS_const_Sc_double_Sg__Sg__Sg__erase__SWIG_2' was not declared in this scope
    $ grep -n --color std_map_Sl_std_string_.*_Sg__erase wrappedmap.cpp
    5172:SWIGINTERN void std_map_Sl_std_string_Sc_double_Sg__erase__SWIG_1(std::map< std::string,double > *self,std::map< std::string,double >::iterator position){ self->erase(position); }
    5173:SWIGINTERN void std_map_Sl_std_string_Sc_double_Sg__erase__SWIG_2(std::map< std::string,double > *self,std::map< std::string,double >::iterator first,std::map< std::string,double >::iterator last){ self->erase(first, last); }
    6974: std_map_Sl_std_string_Sc_double_Sg__erase__SWIG_1(arg1,arg2);
    7025: std_map_Sl_std_string_Sc_double_Sg__erase__SWIG_2(arg1,arg2,arg3);
    8108: std_map_Sl_std_string_Sc_double_Sc_std_less_Sl_std_string_Sg__Sc_std_allocator_Sl_std_pair_Sl_std_string_SS_const_Sc_double_Sg__Sg__Sg__erase__SWIG_1((std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > >*)(arg1)->operator ->(),arg2);
    8159: std_map_Sl_std_string_Sc_double_Sc_std_less_Sl_std_string_Sg__Sc_std_allocator_Sl_std_pair_Sl_std_string_SS_const_Sc_double_Sg__Sg__Sg__erase__SWIG_2((std::map< std::string,double,std::less< std::string >,std::allocator< std::pair< std::string const,double > > >*)(arg1)->operator ->(),arg2,arg3);

     
  • William Fulton

    William Fulton - 2014-03-04

    Your input code is complete junk. Use the 'Formatting Help' button to learn how to put the code into the bug tracker and edit your bug report.

     
  • William Fulton

    William Fulton - 2014-03-07

    I think the code that was posted looked like the following:

    %include <std_string.i>
    %include <std_map.i>
    
    %inline %{
    template <typename T>
    class Wrap {
    T *ptr;
    public:
      Wrap(T *p) : ptr(p) {}
      T const* operator->(void) const { return ptr; }
      T* operator->(void) { return ptr; }
    };
    %}
    
    %template(StringDoubleMap) std::map<std::string, double>; // erase is generated okay
    %template(WrappedMap) Wrap< std::map<std::string, double> >; // erase wrappers lead to compile error
    

    The problem is to do with erase which is overloaded in the definition of std::map including in a %extend. The bug comes about with std::map as it is a template with default arguments and the usage in a smart pointer.
    Fixed for SWIG-3.0.0.

     
  • William Fulton

    William Fulton - 2014-03-07
    • status: open --> closed
    • assigned_to: William Fulton
    • Group: -->
     
  • Martin Skou Andersen

    Yes, that was the code. I tried to format the code, and I am sure I posted it again ... but it does not seem so.

     

Log in to post a comment.