问题
gcc
fails to compile the code below, while clang
compiles ok. I have no control on the macro PORTB
, as it is in a 3rd party library (avr
).
Is it a gcc
bug? How can I work around it in gcc
? As a workaround is somehow possible to create a pre-processor macro which extracts the numerical value from PORTB?
Note this question is similar, but not identical to my previous question.
It is also different from this question, where the developer has the flexibility to change the rhs of the assignment, thus avoiding the reinterpret_cast
.
#include <iostream>
#include <cstdint>
#define PORTB (*(volatile uint8_t *)((0x05) + 0x20))
struct PortB {
static const uintptr_t port = reinterpret_cast<uintptr_t>(&PORTB);
};
int main() {
std::cout << PortB::port << "\n";
return 0;
}
来源:https://stackoverflow.com/questions/54174694/reinterpret-castvolatile-uint8-t37-is-not-a-constant-expression