0

I have an set of registers in MCU, and each of them has an unique adress. Imagine I have some register, that can be described with struct

struct RegisterDefinition{
    u32 v1;
    u32 v2;
    u32 v3;
    u32 v4;
};

I know (from MCU reference manual), the adress that one of the registers of that type has in memory

#define REGISTER_ADRESS 0x12345678

how can I constexpr initialize a pointer or reference of type RegisterDefinition, so I will be able to use that variable in constexpr constructors of other classes?

like

inline constexpr somedef* registerPointer = reinterpret_cast<somedef*>(REGISTER_ADRESS);
4
  • You want &v1 == REGISTER_ADRESS? Commented Oct 13, 2023 at 17:02
  • inline constexpr somedef* registerPointer = reinterpret_cast<somedef*>(REGISTER_ADRESS);or inline constexpr somedef& registerRef = *reinterpret_cast<somedef*>(REGISTER_ADRESS); Commented Oct 13, 2023 at 17:04
  • So you're asking how to workaround the ban on reinterpret_cast in constant expressions? Commented Oct 13, 2023 at 17:05
  • I guess, yes. It seems for my question to be similar to stackoverflow.com/a/10376574/15879231 Commented Oct 13, 2023 at 17:07

1 Answer 1

0

This is currently impossible. The language does not permit it. reinterpret_cast is not permitted in constant expressions and there is no workaround.

Store the integral value of the address in a constexpr variable instead and use e.g. a function that returns a pointer from that integral value by reinterpret_cast as late as possible in whatever calculation you want to do at compile-time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.