我的微控制器项目中有以下 C++11 代码:
template<std::uint32_t... I>
struct mask_or;
template<>
struct mask_or<> {
static constexpr std::uint32_t value = 0;
};
template<std::uint32_t first, std::uint32_t... rest>
struct mask_or<first, rest...> {
static constexpr std::uint32_t value = first | mask_or<rest...>::value;
};
这工作正常,并允许我传递可变数量的 uint32_t 作为模板参数。然后编译器将对它们进行 OR 运算并将每个调用替换为一个常量值。对于微控制器来说,这是理想的,因为它在分配给寄存器之前不必执行 OR 操作。
在某些情况下,我想使用如下枚举类作为值:
enum class gpiopin : std::uint32_t {
p0 = GPIO_IDR_IDR_0, // 0x00000001
...
p14 = GPIO_IDR_IDR_14, // 0x00004000
p15 = GPIO_IDR_IDR_15 // 0x00008000
};
由于我有多个这些枚举类,我正在寻找一种在上述 mask_or 代码中使用枚举类值的通用方法。总之,我希望能够做到这一点:
SFR = mask_or<gpiopin::p1, gpiopin::p14>::value;
理想情况下,我希望 mask_or<...>::value 为 constexpr 以保持低代码大小和高速度。
我觉得这应该是可能的,但我似乎无法让它发挥作用。谁能帮我?