tags:

views:

148

answers:

2

We have some code that looks roughly like this:

// Two enums that differ entirely.
enum A { a1, a2 };
enum B { b1, b2 };

// Functions to convert in some meaningful way between them
A convert(B);
B convert(A);

Now, our compiler goes and does exactly what we expect it to. convert(a1) will call B convert(A), and so on. However, when we use Lint to look at our code, it complains with error 31. We suspect that this is because the underlying integral type of the enums are the same, and thus Lint might be treating them as such.

The question I have is: is the code standard, or is this an accidental use of a compiler feature?

+3  A: 

This code is standard and correct. The bug is in LINT

JaredPar
While I'm of the same opinion, is there any chance you could expand on that a bit? I don't have chapter and verse in front of me :(
Kaz Dragon
A: 

I'm not sure that it would be considered standard to return an enum rather than a typedef'd enum in this way, but I could be wrong. Also, as a C programmer, I would avoid using the same name for the two functions (as overloading is impossible), but I tend to avoid it in C++ anyway as it is easy to make a mistake.

If I were writing this code, I would have something like:

typedef enum { a1, a2 } A;
typedef enum { b1, b2 } B;
A convertBToA(B BInstance);
B convertAToB(A AInstance);

However, I would expect Lint to be happy (in C++) with:

typedef enum { a1, a2 } A;
typedef enum { b1, b2 } B;
A convert(B BInstance);
B convert(A AInstance);
Al
Using the same name makes calling the function from a template easier.
Thomas L Holaday
True. I have used this in the past, but I guess I write so much C code (where it's impossible) that I'm in the habit of having distinct names with everything. Although the MISRA-C++ rules don't preclude overloading, there are a lot of restrictions related to it, so it should be used with care.
Al
A typedef is a synonym for another type. The only significant difference between using the typedef name versus that of the thing you typedef'd is that an object or function won't hide the name of a typedef like they will the type.
Richard Corden