swig/python: making a nested class inside templated class by using declaration

Claudius Schnörr <[email protected]> Mon, 1 Jan 2024 01:33:36 +0100
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============0829940770284564422==
Content-Type: multipart/alternative;
 boundary="------------KMugcFxVbojfjJgp1av7EyQo"

This is a multi-part message in MIME format.
--------------KMugcFxVbojfjJgp1av7EyQo
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Hello,

after swig release 4.2.0 has been submitted, and improvements in=20
'using-declarations' have been stated, I run again a test from some=20
years ago.

I tested whether a nested class 'B' inside a templated class 'A' can be=20
made accessible by a global using statement.
There is no longer a syntax error with swig-4.2.0, however it still=20
doesn't work.
1) Can someone quickly check whether I have overseen something?

The other way would be to make the nested class global and declare it as=20
a nested class by a local using-declaration in A. This works, but=20
requires bigger recodings in the C++-library I wrap to python.
2) is there a timeline for when python support for nested template=20
classes can be expected?

-------------------------------------------------------------------------=
------------------------------------------------------------------------
NestedTemplate.h:
-------------------------

#include <iostream>
using namespace std;

template <typename Tnum>
class A {
public:
 =C2=A0=C2=A0=C2=A0 class B {=C2=A0=C2=A0 //nested class
 =C2=A0=C2=A0=C2=A0 public:
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 B() { cout << "A::B() called"=
 << endl; };
 =C2=A0=C2=A0=C2=A0 };
 =C2=A0=C2=A0=C2=A0 A() { cout << "A() called" << endl; };
};

template <typename Tnum> using Bnest =3D typename A<Tnum>::B;

-------------------------------------------------------------------------=
------------------------------------------------------------------------

NestedTemplate.i:
------------------------

%module NestedTemplate

%feature ("flatnested",1)=C2=A0 A::B;

%header %{
#include "NestedTemplate.h"
%}
%include "NestedTemplate.h"

// explicit instantiations

%template(A_i)=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 A<int>;=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 //ok

//this syntax would be perfect, however not supported yet
//%template(A_i_B)=C2=A0=C2=A0=C2=A0=C2=A0 A<int>::B;=C2=A0 //swig syntax=
 error

//via using alias:
%template(A_B_i)=C2=A0 Bnest<int>; //with swig-4.2.0, no syntax error

-------------------------------------------------------------------------=
------------------------------------------------------------------------

NestedTemplate.py:
---------------------------

#!/usr/bin/env python3

import sys;
sys.path =3D ['../../module/linux'] + sys.path

from NestedTemplate import *

//instatiations
a =3D A_i()=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0 #ok, normal template

b =3D A_B_i()=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 #Name=
Error: name 'A_B_i' is not defined
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 #=
using the top-level Bnest<int> is still=20
not working

Thank you, Claudius

--------------KMugcFxVbojfjJgp1av7EyQo
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF=
-8">
  </head>
  <body>
    <p>Hello, <br>
    </p>
    <p>after swig release 4.2.0 has been submitted, and improvements in
      'using-declarations' have been stated, I run again a test from
      some years ago.</p>
    <p>I tested whether a nested class 'B' inside a templated class 'A'
      can be made accessible by a global using statement.<br>
      There is no longer a syntax error with swig-4.2.0, however it
      still doesn't work.<br>
      1) Can someone quickly check whether I have overseen something?<br>
    </p>
    <p>The other way would be to make the nested class global and
      declare it as a nested class by a local using-declaration in A.
      This works, but requires bigger recodings in the C++-library I
      wrap to python.<br>
      2) is there a timeline for when python support for nested template
      classes can be expected?<br>
    </p>
    <p>------------------------------------------------------------------=
-------------------------------------------------------------------------=
------<br>
      NestedTemplate.h:<br>
      -------------------------<br>
    </p>
    <p>#include &lt;iostream&gt;<br>
      using namespace std;<br>
      <br>
      template &lt;typename Tnum&gt;<br>
      class A {<br>
      public:<br>
      =C2=A0=C2=A0=C2=A0 class B {=C2=A0=C2=A0 //nested class<br>
      =C2=A0=C2=A0=C2=A0 public:<br>
      =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 B() { cout &lt;&lt; "A::=
B() called" &lt;&lt; endl; };<br>
      =C2=A0=C2=A0=C2=A0 };<br>
      =C2=A0=C2=A0=C2=A0 A() { cout &lt;&lt; "A() called" &lt;&lt; endl; =
};<br>
      };<br>
      <br>
      <font color=3D"#0000ff">template &lt;typename Tnum&gt; using Bnest =
=3D
        typename A&lt;Tnum&gt;::B;</font><br>
    </p>
    <p>------------------------------------------------------------------=
-------------------------------------------------------------------------=
------</p>
    <p>NestedTemplate.i:<br>
      ------------------------<br>
    </p>
    <p>%module NestedTemplate<br>
      <br>
      %feature ("flatnested",1)=C2=A0 A::B;<br>
      <br>
      %header %{<br>
      #include "NestedTemplate.h"<br>
      %}<br>
      %include "NestedTemplate.h"<br>
      <br>
      // explicit instantiations<br>
      <br>
      %template(A_i)=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 A&lt;int&gt;;=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 //ok<br>
      <br>
      //this syntax would be perfect, however not supported yet<br>
      //%template(A_i_B)=C2=A0=C2=A0=C2=A0=C2=A0 A&lt;int&gt;::B;=C2=A0 /=
/swig syntax error<br>
      <br>
      //via using alias:<br>
      <font color=3D"#0000ff">%template(A_B_i)=C2=A0 Bnest&lt;int&gt;;=C2=
=A0</font>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0
      //with swig-4.2.0, no syntax error<br>
    </p>
    <p>------------------------------------------------------------------=
-------------------------------------------------------------------------=
------</p>
    <p>NestedTemplate.py:<br>
      ---------------------------<br>
    </p>
    <p>#!/usr/bin/env python3<br>
      <br>
      import sys;<br>
      sys.path =3D ['../../module/linux'] + sys.path<br>
      <br>
      from NestedTemplate import *<br>
    </p>
    <p>//instatiations<br>
      a =3D A_i()=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0 #ok, normal template<br>
      <br>
      b =3D A_B_i()=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 #NameError: name 'A_B_i' is not defined<br>
      =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 #using the top-level Bnest&lt;int&gt;
      is still not working<br>
    </p>
    <p>Thank you, Claudius<br>
    </p>
    <p>
    </p>
  </body>
</html>

--------------KMugcFxVbojfjJgp1av7EyQo--


--===============0829940770284564422==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============0829940770284564422==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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

--===============0829940770284564422==--