This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Struct Enum support for Keil compiler

Hi,
Need information on support of scoped enums in Keil compiler.
My final objective is to use the Enums as AIAction:Enter</i or AIAction:Exit instead of plain Enter or Exit in the source code C files.
This is to solve the global namespace pollution.

I have the following structure in header file :

struct AIAction{
    enum AIActionType
    {
        Enter,
        Exit,
        Run,
    };
};

For using it, following is the code in C file (filetype is C)

    int iType;
    iType = AIAction::Run;

When I compile this with current settings, it is giving me error #618: struct or union declares no named members

However, if I change the filetype to C++, it compiles successfully. So basically C++ compiler is allowing such structure definition.

From this, I infer that C has got limitation of not allowing any non-named members in structures.

How do I use the enums the way I want? Can this be achieved without using C++ compiler?

  • If you are willing to write AIAction::Run in all of the software, then you can just as well replace : with _ and write AIAction__Run.

    It isn't actually the number of global symbol names that is a reason why global symbol names should be kept down. It's more the complexity of trying to figure out what symbol names are irrelevant when doing a specific operation.

    That's also why a traditional C program normally uses function names that implies some form of namespace, so uart0_init(), uart0_send(), ... while the C++ code would instead have had an object uart0 with member methods.

  • Thanks Per for your reply and explanation.

    Other than keeping the code less polluted with enums, I also intend to keep the enums in their own namespaces. This will actually help during code development without searching for Enum names. Your solution of affixing the EnumType_ will also work and feasible but then again code may not look that good.

    I have developed C++ applications in Qt framework and really liked how the enums are handled in class scope. Same thing I wanted to implement with C and just learned that it is not possible with current Keil compiler.

    So, either I implement your proposed solution in C or use the namespace like below and use the C++ compiler. I can also use the struct definition as in 1st post.

    namespace AIAction {
        enum AIActionType
        {
            Enter,
            Exit,
            Run,
        };
    }
    

  • Note that if it's enough to add a data member to the struct to make the compiler happy, then this will not cost you any memory space, since you do not intend to instantiate the struct.

  • Sorry Per. Didn't understand what you meant.

  • You complained about the error message:
    "When I compile this with current settings, it is giving me error #618: struct or union declares no named members"

    If you modify to:

    struct AIAction {
        enum AIActionType
        {
            Enter,
            Exit,
            Run,
        } dummy;    // <= make sure there is a named member
    };
    


    Then you do have a named member, so the compiler error message should go away.
    And it doesn't cost you any RAM, since you will not instantiate any variable of type struct AIAction.

  • Yes Per. I did that and I couldn't get anymore error. But then, I cannot use AIAction::Enter because then you will be required to create variable of struct AIAction type.

  • My final objective is to use the Enums as AIAction::Enter or AIAction::Exit instead of plain Enter or Exit in the source code C files.

    [Note: some typos corrected in the above]

    That objective is impossible, because those idioms cannot do not exist in C. Files that contain such idioms are, by definition, not C source files; they're C++ source files.

    From this, I infer that C has got limitation of not allowing any non-named members in structures.

    You inferred incorrectly. The problem is not that this is an unnamed member. The problem is that the thing you're trying to define is not actually a member of the struct at all --- it just becomes part of its namespace scope. The error messages that lead you to that incorrect conclusion is based on the fact that this is C++-only syntax, which led the C compiler totally astray. BTW C does allow unnamed members, as of the latest revision of the Standard in 2011, but those must be structs or unions themselves.

    There are people who claim to be teaching a programming language by the name of "C/C++". Well, sorry, but that language does not exist, and it never really did. C and C++ are different languages. They do have a common subset, but that's become smaller and less useful with each of the last decade or two's worth of updates to the both language definitions. So you'll have to make up your mind which of them you're going to use, and stick with that decision.

  • Hey Hans, thanks for correcting. Really appreciate it.