5
我使用下面的代码来访问一些MCU寄存器。g ++中的constexpr函数的不同行为 - 7.0 /访问硬件
#include <stdint.h>
struct MCU {
struct Timer {
volatile uint8_t r1;
template<int N> struct Address;
};
};
template<>
struct MCU::Timer::Address<0> {
static constexpr uint8_t value = 0x25;
};
template<typename Component, int Number>
constexpr Component* getBaseAddr() {
return reinterpret_cast<Component*>(Component::template Address<Number>::value);
}
struct Test {
static void foo() {
p->r1 = 42;
}
static constexpr auto p = getBaseAddr<MCU::Timer, 0>();
};
int main() {
Test::foo();
while(true) {}
}
在AVR-克++ 6.2.1能正常工作。但是现在有了AVR-G ++ 7.0我得到的错误:
in constexpr expansion of 'getBaseAddr<MCU::Timer, 0>()'
bm10a.cc:23:58: error: value '37u' of type 'MCU::Timer*' is not a constant expression
static constexpr auto p = getBaseAddr<MCU::Timer, 0>();
我得出的结论,该版本6.2.1不confroming和7.0! reinterpret_cast会导致非constexpr表达式。
那么,有没有解决方案宣布一个注册地址为constexpr?
此生成对我在C++ 11,C++ 14和C++采用bog标准GCC 6.2.0的1z模式。 [但GCC 7的C++ 17支持更彻底](https://gcc.gnu.org/projects/cxx-status.html#cxx1z)。你能澄清你正在尝试使用哪种语言?然后我们可以追踪这是否C++本身已经改变了。 –
挂上...这段代码里面没有43行...你在告诉猪肉吗?请告诉我,我不仅浪费了我周五晚上20分钟的时间! –
另外GCC 7只是预发行版本,所以你使用的是_actual_版本?给出构建日期或'-v'输出。 –