Различное поведение с функцией constexpr в g ++ — 7.0 / доступ к оборудованию

Я использую следующий код, чтобы получить доступ к некоторым регистрам MCU.

#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-g ++ 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 не противоречит, а 7.0 — это! reinterpret_cast приводит к выражениям non-constexpr.

Итак, есть ли решение объявить адрес регистра как constexpr?

5

Решение

Возможное решение в соответствии с phil1970 будет использовать getBaseAddr () в качестве встроенной функции или псевдоним для этого:

struct Test {
static void foo() {
p()->r1 = 42;
}
static constexpr auto p = getBaseAddr<MCU::Timer, 0>;
};
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector