Enum vs Strongly typed enum

后端 未结 5 1822
长情又很酷
长情又很酷 2020-11-29 18:44

I am a beginner in C++ programming.

Today I come across a new topic: strongly typed enum. I\'ve researched it a bit but till now I am unable to find ou

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 19:13

    Values of enum class is really of type enum class, not underlying_type as for C-enums.

    enum xyz { a, b, c};
    enum class xyz_c { d, f, e };
    
    void f(xyz x)
    {
    }
    
    void f_c(xyz_c x)
    {
    }
    
    // OK.
    f(0);
    // OK for C++03 and C++11.
    f(a);
    // OK with C++11.
    f(xyz::a);
    // ERROR.
    f_c(0);
    // OK.
    f_c(xyz_c::d);
    

提交回复
热议问题