Menu

#1284 %template and %ignore don't match when template parameter

open
nobody
5
2022-03-18
2012-11-27
No

Considering a C++ class like

template <uint32_t Size, typename StorageType>
class Vector
{
public:
Vector(StorageType x, StorageType y);
Vector(StorageType x, StorageType y, StorageType z);
Vector(StorageType x, StorageType y, StorageType z, StorageType w);
...
};

I wrap it in SWIG using

%module Vector
%{
#include "Vector.h"
%}

%include "stdint.i"
%include "Vector.h"

%ignore Vector(int8_t, int8_t);
%ignore Vector(int8_t, int8_t, int8_t, int8_t);
%template(Vector3DInt8) PolyVox::Vector<3,int8_t>;

So that the 2- and 4-parameter constructors will be ignored.

However, what seems to be happening is the %template macro is reducing the int8_t typedef down to 'signed char' and then when doing the %ignore, it is failing to match it since I guess the %ignore isn't also doing typedef reduction?

I even tried using the '-notemplatereduce' SWIG argument but it appeared to have no effect as running SWIG with '-debug-template' gives

Vector.i:27: template_debug: Searching for match to: 'Vector<(3,int8_t)>'
searching for : 'Vector<(3,int8_t)>' (explicit specialization)
searching for : 'Vector<(3,signed char)>' (explicit specialization with typedef reduction)
matched partials: NONE
chosen primary template: 'Vector'
chosen template:'Vector'

and still the %ignore did not work as expected.

Discussion

  • William Fulton

    William Fulton - 2012-12-04

    This bug is related to a few template and typedef problems. The code is inconsistent as to when it does typedef reduction and the fix does not look particularly simple. Currently the "decl" attribute in the node is typedef reduced and hence added into the symbol tables as such. The %rename does not do the appropriate typedef reduction (%feature too) so there is no match.

    Currently the following will work because of the typedef reduced parameters:

    %ignore Vector(signed char, signed char);
    %ignore Vector(signed char, signed char, signed char, signed char);

    or fully qualified class:

    %ignore PolyVox::Vector<3, int8_t>::Vector(signed char, signed char);
    %ignore PolyVox::Vector<3, int8_t>::Vector(signed char, signed char, signed char, signed char);

    For future reference the relevant code is in parser.y add_symbols_copy() which calls add_symbols() which in turn calls make_name().

     
  • Olly Betts

    Olly Betts - 2022-03-18

    Reproducible with SWIG git master - all 3 constuctors are wrapped with -c++ -php.

    I used the testcase above, but added namespace PolyVox { ... } in Vector.h around everything else.

     

Log in to post a comment.