Is it possible to specialize a template using a member enum?

前端 未结 2 1530
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 05:44
struct Bar {
  enum { Special = 4 };
};

template struct Foo {};
template struct Foo {};

U

2条回答
  •  难免孤独
    2020-12-29 06:25

    Since that is not allowed by C++ as explained by Prasoon, so an alternative solution would be to use EnumToType class template,

    struct Bar {
      enum { Special = 4 };
    };
    
    template
    struct EnumToType
    {
      static const int value = e;
    };
    
    template //note I changed from "int K" to "class K"
    struct Foo
    {};
    
    template 
    struct Foo > 
    {
       static const int enumValue = T::Special;
    };
    

    Sample code at ideone : http://www.ideone.com/JPvZy


    Or, you can simply specialize like this (if it solves your problem),

    template struct Foo {};
    
    //usage
    Foo f;
    

提交回复
热议问题