我正在抽象多个微 Controller 上的中断 vector 表。
我正在使用以下形式的模板类
(InterruptVectorTable.hpp(定义,包含在实现中))
template<class Device, class ResultType>
InterruptVectorTable
{
enum class IRQType : ResultType;
}
Device
是一种虚拟类,用于专门化模板。class DeviceAtMega328p
{
public:
static const int s_NumInterruptVectors = {26};
};
(这里我仍在考虑是否将26作为模板参数或以这种形式传递。)
由于每个微 Controller 都有自己的中断类型和值,应在编译时进行检查(由于枚举类),因此我还要专门介绍这种形式的特定中断(InterruptVectorTable.hpp(Implementation):
template<>
InterruptVectorTable< DeviceAtMega328p, uint8_t>
{
enum class IRQType : ResultType
{
//RESET_IRQn = 0, // Not available.
INT0_IRQn = 1,
INT1_IRQn = 2,
PCINT0_IRQn = 3,
PCINT1_IRQn = 4,
PCINT2_IRQn = 5,
WDT_IRQn = 6,
// .....
};
}
这种方法似乎无法按预期方式工作(当前有太多错误无法指定,而明确指出了这一部分)。
最佳答案
首先,这不是您引入新模板类的方式:
// Wrong!
template<>
InterruptVectorTable<class Device, class ResultType>
{
// ...
}
正确的方法:
template<class Device, class ResultType>
class InterruptVectorTable
{
// ...
};
其次,您不能在模板专业中必须定义的模板中“向前声明”某些内容。专门知识一无所知,并且与“基本案例”毫无共同之处。
您只需执行以下操作即可使用您的代码:
template<>
class InterruptVectorTable<DeviceAtMega328p, uint8_t>
{
enum class IRQType : uint8_t
{
//RESET_IRQn = 0, // Not available.
INT0_IRQn = 1,
INT1_IRQn = 2,
PCINT0_IRQn = 3,
PCINT1_IRQn = 4,
PCINT2_IRQn = 5,
WDT_IRQn = 6,
//...........
};
};
InterruptVectorTable
的用户将简单地假设每个专业都定义了enum class IRQType
。没有直接的方法强制特化这样做。对于IRQType
以ResultType
作为底层类型的要求,情况与此相同。
关于c++ - 如何将枚举类声明为模板类的内部类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59669178/