I modified the question here. Now it looks muck like a answer, :). Thank you all for sort this out.
I have some overloaded constructor in my class, Like this
#include <string>
#ifdef EXPLICIT_ENUM_CONVERSION
enum struct E
#else
enum E
#endif
{
A,
B
};
class A
{
public:
A(unsigned int i){};
A(std::string const& s){};
};
And a function declaration that actually accept A as a parameter
void func(const class A& a)
{
}
But the caller is invoking it by passing a enum
.
int main()
{
#ifdef EXPLICIT_ENUM_CONVERSION
E e=E::A;
func((unsigned int)e);
#else
E e=A;
func(e);
#endif
}
Question: By commenting out the A(unsigned)
and compile again to get an error, I can tell that the constructor that is used. But is there a better way to tell how the types are converted from the gcc
command line or objdump
the result?
Answer: If compiling with -O0 and then use objdump -CSr, the class constructor used is shown from the objdump.
Question: is there any way to prevent the enum
to unsigned
automatic conversion with gcc?
Answer: See the answer I picked. The scoped enum is introduced in C++11 and can satisfy the purpose. You can check the code within EXPLICIT_ENUM_CONVERSION.