python: STL containers and enum types
Alessio M <[email protected]> Wed, 18 Jun 2025 12:53:51 +0100
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CAL_VXbbk3pB2FsopOQMOxeg6-PSvNnkyK7bBtuh4eHfOR0CJPQ@mail.gmail.com> |
--===============3734393351157816615==
Content-Type: multipart/alternative; boundary="0000000000009afe200637d74a3d"
--0000000000009afe200637d74a3d
Content-Type: text/plain; charset="UTF-8"
Hi there!
I've been having issues with containers that use enums as elements or keys
I can see the code for the container being generated correctly
But the typecheck fails because no descriptor can be found for the enum
type at the following line of code
https://github.com/swig/swig/blob/master/Lib/std/std_common.i#L105
I understand that the enum is seen by python as a simple integer.
Do I need to add anything specific to make them work as keys in maps or
elements in vectors?
Thank you!
enum class FooEnum {
one,
two
};
double bar(std::unordered_set<FooEnum> const&)
{
return 1;
}
double bar(std::unordered_map<FooEnum, int> const&)
{
return 1;
}
%template(FooSet) std::unordered_set<FooEnum>;
%template(FooMap) std::unordered_map<FooEnum, int>;
--0000000000009afe200637d74a3d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>Hi there!</div><div><br></div><div>I've been havi=
ng issues with containers that use enums as elements or keys</div><div>I ca=
n see the code for the container being generated correctly</div><div>But th=
e typecheck fails because no descriptor can be found for the enum type at t=
he following line of code</div><div><a href=3D"https://github.com/swig/swig=
/blob/master/Lib/std/std_common.i#L105">https://github.com/swig/swig/blob/m=
aster/Lib/std/std_common.i#L105</a></div><div><br></div><div>I understand t=
hat the enum is seen by python as a simple integer.</div><div>Do I need to =
add anything specific to make them work as keys in maps or elements in vect=
ors?</div><div><br></div><div>Thank you!</div><div><br></div>enum class Foo=
Enum=C2=A0{<div>=C2=A0 one,</div><div>=C2=A0 two<br><div>};<br><div>double =
bar(std::unordered_set<FooEnum> const&)</div></div></div><div>{</=
div><div>=C2=A0 return 1;</div><div>}</div><div><div><div><div>double bar(s=
td::unordered_map<FooEnum, int> const&)</div></div></div><div>{</=
div><div>=C2=A0 return 1;</div><div>}</div><div></div></div><div>%template(=
FooSet) std::unordered_set<FooEnum>;</div><div>%template(FooMap) std:=
:unordered_map<FooEnum, int>;</div></div>
--0000000000009afe200637d74a3d--
--===============3734393351157816615==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
--===============3734393351157816615==
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
--===============3734393351157816615==--