7

This is written by someone who has left the company. I can't see any reason to do this and am curious if there's something I am missing.

enum thing_type_e
{
   OPTION_A = 0,
   OPTION_B,
   OPTION_C,
   OPTION_D
};

struct thing_type_data_s
{
   enum_type_e mVariable;
};

I supposed it's possible he was going to add more to the structure, but after looking at how it is used, I don't think so.

Barring "he was going to add more to the structure," why package a single enum in a struct? Is there some motivation I'm not thinking of?

Update:

As asked in the comments, he used it in this fashion:

void process_thing_type(thing_type_data_s* ParamVariable)
{
    local_variable = ParamVariable->mVariable;
    ...
}

This was originally built with GCC 3.3.5 if it makes any difference.

5
  • How the structure is used? Can you provide some examples? Commented Jan 23, 2014 at 20:38
  • I have seen this done before as well - I can't recall but I think it had something to do with simplifying debugging - you can see the possible values enumerated in the debugger maybe? Commented Jan 23, 2014 at 20:39
  • @WojtekSurowka Updated the question with an example... but it's really nothing special. I wonder if it is so he could see things better in GDB. Commented Jan 23, 2014 at 20:44
  • Possibly he just wasn't sure it was legal to pass a pointer to an enum. Enums definitely seem different from other types, I can see someone doing this to "play it safe". Commented Jan 23, 2014 at 20:51
  • I guess that the usage confirms @juanchopanza answer Commented Jan 23, 2014 at 20:53

2 Answers 2

13

Possibly to enforce some type safety. Old-style enums are implicitly convertible to integral types, and this is not always desirable. Besides this, they are unscoped.

C++11 adds scoped enumerations (or "class" enums) to fix both of these issues.

Here's an example:

void foo(int) {}

int main()
{
  foo(OPTION_A);  // OK
  thing_type_data_s s = { OPTION_A };
  foo(s);  // Error
}
Sign up to request clarification or add additional context in comments.

6 Comments

I think you missed that the enum declaration isn't nested in the struct. This isn't anything like what you're saying.
@TimothyShields No, I didn't miss that. The main point is the implicit conversion to integral types. The scoping is another, unrelated, matter.
juancho wrote the same thing I thought of. Perhaps that guy thought "let's use the struct instead of the plain enum", without visibility scopes. Who knows..
I probably should have mentioned that he wrote it and built it with GCC 3.3.5.
@KeithThompson an old-schoole enum values are in the enclosing scope. So enum Colour { Blue, Orange }; enum Fruit { Apple, Orange }; is an error. That is what I was referring to.
|
0

Are there other structs with the same type of member first but different otherwise? He might be using the struct thing_type_data_s in a baseclass-y kind of way. But who knows, you tagged the question as both C and C++. I would make sense in C, at least.

1 Comment

No, there aren't. It was just this one struct. Thanks for the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.