Link to home
Start Free TrialLog in
Avatar of OliverRudolph
OliverRudolphFlag for Germany

asked on

SWIG: Shadowed c'tors using different enums

Hi folks!

Just recently I have begun using SWIG to create an Python interface to my C++ kernel. Now, I feel a little overwhelmed by the possibilities and cannot find the right place to find a solution for the following problem of mine:

I have two different enums and a class with different constructors making use of them, e.g.

enum A { a1, a2, a3 };
enum B { b1, b2, b3 };

class C
    C(string s, A a);
    C(string s, B b);
    C(string s, A a, B b);

Now when using SWIG on this setup, it gives me a message on the shadowing of c'tor C(string s, B b) by C(string s, A a) as apparently Python doesn't know about enumerations and replaces them with integer values. As it is (obviously) impossible to distinguish between C(string s, int a) and C(string s, int b), the second c'tor cannot be called from Python.

I've seen several entries in the SWIG manuals about typemaps, but I am really not sure how to use them or if they can be applicable and helpful in this case (it's clearly stated, typemaps are of the more advanced topics in SWIG interface generation).

Can anybody help me to resolve this issue?

The use of enumerations in C++ is made this way to enable the use of default parameters without paying respect to the ordering of parameters.

Thank you very much in advance and
best regards!

Avatar of evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of OliverRudolph


Hi evilrix,

thanks for the very fast response!!!

I was just thinking about quite the same thing as I found some indication of this here: I'll give it a try and reward you the points if it works (which I have no doubt about :)

Thank you!

The idea here is to make the enum type strongly typed. Although an enum value is a unique type in C/C++ it will decompose to an int type automatically, the trick is to prevent that. The solution is to wrap it in something that can't.

IMO, enum types are nearly practically useless in C/C++ (with the one exception that they are useful for template meta-programming)... the one logical thing you can't do with them is enumerate them - duh! :) They are basically, integer types on steroids.

Rant ends. >:)