Forward-declare enum in Objective-C

前端 未结 6 2354
陌清茗
陌清茗 2020-11-30 02:06

I\'m having trouble with enum visibility in an Objective-C program. I have two header files, and one defines a typedef enum. Another file needs to use the

6条回答
  •  离开以前
    2020-11-30 02:52

    If you are ok using compiler extensions, you could use this order in Clang:

    enum Enum;
    typedef enum Enum Enum2;
    
    void f(Enum2); // ok. it sees this type's true name.
    
    enum Enum {
        E_1
    };
    
    // ok. now its declaration is visible and we can use it.
    
    void f(Enum2 e) {
    
    }
    

    Note: It will trigger a -Wpedantic warning.


    If you are using C++11, you should use their enums, which are safe to forward declare -- e.g. enum class Enum:uint8_t; (not a compiler extension).

提交回复
热议问题