Avatar of OliverRudolph
OliverRudolph
Flag 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!

Oliver
PythonC++

Avatar of undefined
Last Comment
evilrix

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
evilrix

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
OliverRudolph

ASKER
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: http://www.swig.org/Doc1.3/SWIGPlus.html#SWIGPlus_nn16 I'll give it a try and reward you the points if it works (which I have no doubt about :)

Thank you!

Oliver
evilrix

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. >:)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck