Re: swig/python: making a nested class inside templated class accessible by using declaration
William S Fulton <[email protected]> Wed, 14 Feb 2024 07:58:57 +0000
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CANGqftDdQbmgBtsb02Gi2vTy5m8ZEKcPmpbJX+P+PXa29k3_3w@mail.gmail.com> |
--===============6294498250691489912== Content-Type: multipart/alternative; boundary="0000000000005823fa061152e4a8" --0000000000005823fa061152e4a8 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, 1 Jan 2024 at 20:35, Claudius Schn=C3=B6rr <[email protected]> = wrote: > Subject corrected -> 'accessible' > > ------------------------------------------- > > Hello, > > 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. > > I tested whether a nested class 'B' inside a templated class 'A' can be > made accessible by a global using statement. > There is no longer a syntax error with swig-4.2.0, however it still > 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 = a > nested class by a local using-declaration in A. This works, but requires > bigger recodings in the C++-library I wrap to python. > 2) is there a timeline for when python support for nested template classe= s > can be expected? > > > -------------------------------------------------------------------------= ------------------------------------------------------------------------ > NestedTemplate.h: > ------------------------- > > #include <iostream> > using namespace std; > > template <typename Tnum> > class A { > public: > class B { //nested class > public: > B() { cout << "A::B() called" << endl; }; > }; > A() { cout << "A() called" << endl; }; > }; > > template <typename Tnum> using Bnest =3D typename A<Tnum>::B; > > > -------------------------------------------------------------------------= ------------------------------------------------------------------------ > > NestedTemplate.i: > ------------------------ > > %module NestedTemplate > > %feature ("flatnested",1) A::B; > > %header %{ > #include "NestedTemplate.h" > %} > %include "NestedTemplate.h" > > // explicit instantiations > > Rename the wrapped nested class as SWIG calls the generated nested class just B. %rename(A_B_i) A<int>::B; > %template(A_i) A<int>; //ok > > //this syntax would be perfect, however not supported yet > //%template(A_i_B) A<int>::B; //swig syntax error > > //via using alias: > %template(A_B_i) Bnest<int>; //with swig-4.2.0, no syntax error > The %template above doesn't do anything as this is not what should be instantiated. The one and only thing to instantiate is the template A<int> and that has already been instantiated above. I guess it ought to warn or error out though. The instantiation named A_i generates the nested B class, named simply as B, though. I'd raise a bug report in Github about the nested class being called A instead of say A_B_i though by default. I think that might be a bug. William --0000000000005823fa061152e4a8 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div dir=3D"ltr"><br></div><br><div class=3D"gmail_quote">= <div dir=3D"ltr" class=3D"gmail_attr">On Mon, 1 Jan 2024 at 20:35, Claudius= Schn=C3=B6rr <<a href=3D"mailto:[email protected]">schnoerr@mailzon= e.com</a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"ma= rgin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:= 1ex"> =20 =20 =20 <div> <p>Subject corrected -> 'accessible'</p> <p>-------------------------------------------<br> </p> <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 fro= m 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 <iostream><br> using namespace std;<br> <br> template <typename Tnum><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 << "= A::B() called" << endl; };<br> =C2=A0=C2=A0=C2=A0 };<br> =C2=A0=C2=A0=C2=A0 A() { cout << "A() called" <<= ; endl; };<br> };<br> <br> <font color=3D"#0000ff">template <typename Tnum> using Bnest = =3D typename A<Tnum>::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></p></div></blockquote><div>Rename the wrapped nested class as SW= IG calls the generated nested class just B.<br></div><div>%rename(A_B_i) A&= lt;int>::B;<br>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"ma= rgin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:= 1ex"><div><p> %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<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<int>::B;=C2=A0 //s= wig syntax error<br> <br> //via using alias:<br> <font color=3D"#0000ff">%template(A_B_i)=C2=A0 Bnest<int>;=C2= =A0</font>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 //with swig-4.2.0, no syntax error<br></p></div></blockquote><div>The= %template above doesn't do anything as this is not what should be inst= antiated. The one and only thing to instantiate is the template A<int>= ; and that has already been instantiated above. I guess it ought to warn o= r error out though. The instantiation named A_i generates the nested B clas= s, named simply as B, though.<br></div><div>=C2=A0</div>I'd raise a bug= report in Github about the nested class being called A instead of say A_B_= i though by default. I think that might be a bug.=C2=A0</div><div class=3D"= gmail_quote"><br></div><div class=3D"gmail_quote">William<br></div></div> --0000000000005823fa061152e4a8-- --===============6294498250691489912== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline --===============6294498250691489912== 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 --===============6294498250691489912==--