Re: problems using templates in test code?

Marko Klopcic <[email protected]> Thu, 2 Nov 2023 14:58:30 +0100
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
%template directive defines type to be used in target language, not
C++. For example, in Python

you can write:

v = vector_string(); // SWIG defines Python type as vector of
strings.

For C++ you have to define alias or typedef using C++ syntax, or use
plain std::vector<std::string>.

Example:

using vector_string
= std::vector<std::string>;

Marko

On 30 Oct 2023 16:31, Meehan, Tim
wrote:

I
was trying to use a std::vector<std::string> with a
template instantiation and got a lot of compilation errors.

The header file for the test code is:

#include <string>

#include
<vector>

std::vector<std::string>
get_list();

void
set_list(std::vector<std::string> s);

The
swig file is:

%module test

%{

#include
"test.h"

%}

%include
"std_string.i"

%include
"std_vector.i"

vector_string
get_list();

void
set_list(vector_string s);

%template(vector_string)
std::vector<std::string>;

I
tried to generate code with:

#!/usr/bin/sh

swig
-Wall -c++ -python test.i

ALLFILES=$(ls
*cpp *cxx)

g++
-Wall -fPIC -shared $ALLFILES -o _test.so
-I/usr/include/python3.10/

However,
this generates the usual voluminous C++ error dump that I
can't figure out. It seems to me I have instantiated the
template already and it should be in the wrapper file already?

test_wrap.cxx: In function ‘PyObject*
_wrap_get_list(PyObject*, PyObject*)’:

test_wrap.cxx:5714:3:
error: ‘vector_string’ was not declared in this scope

5714
| vector_string result;

| ^~~~~~~~~~~~~

test_wrap.cxx:5717:3:
error: ‘result’ was not declared in this scope; did you mean
‘resultobj’?

5717
| result = get_list();

| ^~~~~~

| resultobj

test_wrap.cxx:5718:39:
error: expected type-specifier before ‘vector_string’

5718
| resultobj = SWIG_NewPointerObj((new
vector_string(static_cast< const vector_string&
>(result))), SWIGTYPE_p_vector_string, SWIG_POINTER_OWN |
0 );

| ^~~~~~~~~~~~~

test_wrap.cxx:1092:89:
note: in definition of macro ‘SWIG_NewPointerObj’

1092
| #define SWIG_NewPointerObj(ptr, type, flags)
SWIG_Python_NewPointerObj(NULL, ptr, type, flags)

|
^~~

test_wrap.cxx:5718:39:
error: expected ‘)’ before ‘vector_string’

5718
| resultobj = SWIG_NewPointerObj((new
vector_string(static_cast< const vector_string&
>(result))), SWIGTYPE_p_vector_string, SWIG_POINTER_OWN |
0 );

| ~ ^~~~~~~~~~~~~

test_wrap.cxx:1092:89:
note: in definition of macro ‘SWIG_NewPointerObj’

1092
| #define SWIG_NewPointerObj(ptr, type, flags)
SWIG_Python_NewPointerObj(NULL, ptr, type, flags)

|
^~~

test_wrap.cxx:
In function ‘PyObject* _wrap_set_list(PyObject*, PyObject*)’:

test_wrap.cxx:5727:3:
error: ‘vector_string’ was not declared in this scope

5727
| vector_string arg1 ;

| ^~~~~~~~~~~~~

test_wrap.cxx:5742:23:
error: ‘temp’ was not declared in this scope; did you mean
‘tm’?

5742
| vector_string * temp = reinterpret_cast<
vector_string * >(argp1);

| ^~~~

| tm

test_wrap.cxx:5742:48:
error: ‘vector_string’ does not name a type

5742
| vector_string * temp = reinterpret_cast<
vector_string * >(argp1);

|
^~~~~~~~~~~~~

test_wrap.cxx:5742:62:
error: expected ‘>’ before ‘*’ token

5742
| vector_string * temp = reinterpret_cast<
vector_string * >(argp1);

|
^

test_wrap.cxx:5742:62:
error: expected ‘(’ before ‘*’ token

5742
| vector_string * temp = reinterpret_cast<
vector_string * >(argp1);

|
^

|
(

test_wrap.cxx:5742:64:
error: expected primary-expression before ‘>’ token

5742
| vector_string * temp = reinterpret_cast<
vector_string * >(argp1);

|
^

test_wrap.cxx:5742:72:
error: expected ‘)’ before ‘;’ token

5742
| vector_string * temp = reinterpret_cast<
vector_string * >(argp1);

|
^

|
)

test_wrap.cxx:5743:7:
error: ‘arg1’ was not declared in this scope; did you mean
‘argp1’?

5743
| arg1 = *temp;

| ^~~~

| argp1

test_wrap.cxx:5744:39:
error: type ‘<type error>’ argument given to ‘delete’,
expected pointer

5744
| if (SWIG_IsNewObj(res1)) delete temp;

| ^~~~

test_wrap.cxx:5747:12:
error: ‘arg1’ was not declared in this scope; did you mean
‘argp1’?

5747
| set_list(arg1);

| ^~~~

| argp1

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user