Using enum as template type argument in C++

前端 未结 3 1966
一生所求
一生所求 2021-02-07 09:29

are there any restrictions / problems using an enum as template (type) argument in C++?

Example:

enum MyEnum
{
    A, B, C, D, E
};

template 

        
3条回答
  •  难免孤独
    2021-02-07 10:17

    Yes, there are restrictions. For example, you cannot use an anonymous enum as a template argument according to C++03 14.3.1[temp.arg.type]/2

    A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

    So the following code is not valid in C++03:

    template 
    void f(T) {}
    
    enum {A};
    
    int main() {
      f(A);
    }
    

    It is valid in C++11 though.

提交回复
热议问题