enums in C++

Nathan Sidwell nathan@codesourcery.com
Tue Apr 18 09:21:00 GMT 2000


Ulrich Drepper wrote:
> Yes, it must be valid C++ but the rules must be relaxed.  It must be
> possible to call the functions the way you can do this in C.  If a
> function is defined which C linkage we already have to treat certain
> things special and I think enum belong into this category.  Their
Unless I'm missing something, only the name mangling is different, and
that's something that happens well after overload resolution. (It's
possible for a different calling convention too, but that also is after
overload resolution.)

> handling is much more relaxed in C and this is what people are using.
If I understand you correctly, you want an extension to C++.

> It simply does not help insisting that they write C++.  You have to
> use C libraryies at some point and then you have to use their
> interface.  If the interface is defined using enum of bit values then
> people can reasonably expect it to work just as in C.
You're saying you'd like code of the form
	extern "C" int someCfunc (enum ex_enum);
	...
	someCfunc (enumA | enumB);
to work as if it were C? rather than
`someCfunc (ex_enum (enumA | enumB))'?

This leads to an ambiguity in the following,
	extern "C" int foo (enum ex_enum);	//1
	extern int foo (int);			//2
	foo (enumA | enumB);
which should be called? C++ requires that 2 be called. What about
	extern "C" int foo (enum ex_enum);	// 1
	template<class T> int foo (T);		// 2
	foo (enumA | enumB);
Here C++ requires template instantiation of foo<int>.

> This is apparently how other C++ work.  Otherwise this code wouldn't
What do the other compilers do with the above examples?

> be in such wide use.  At least relax the rules when using
> -fpermissive.
This is complicated, if the set of overloaded functions has
exactly one member, then it might be permissible. But there are
other places where one might want this behaviour. i.e
	enum ex_enum flags = enumA;
	flags |= enumB;				// bang!
	flags = enumA | enumB;			// bang!
Should this also be allowed in an extern "C" region? Allowing implicit
int->enum conversion in one place and not others is liable to confuse
people, IMO. Let's not add another exception to the rules!

It might be that the enum rules changed during C++'s spec, and that
the compilers that you've found to accept it, are doing so `by
accident'.

nathan

-- 
Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
         'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org


More information about the Gcc-bugs mailing list