I have following class:
class Bit
{
void * bb_addr;
public:
constexpr Bit ( void * __data, int bitpos )
: bb_addr((void*)(0x22000000 + ((uint32_t)(__data) - 0x20000000) * 32 + bitpos * 4))
{}
};
In this class definition I used constexpr constructor - simply I want to create object at compile time. This is requirement, because I write for microcontrollers, I have quite lot of program code memory, but not much of RAM.
Expression in constructor is calculation of bit-band address for specified bit - I would like to use this functionality on microcontroller.
In another place objects of this class are defined with static constexpr
modifier such as static constexpr Bit(&somevar, 15);
so finally it was constant object in FLASH memory. This is my target.
Unfortunately, this is rejected by compiler due to non constant expression. This make me consider - it is possible to use this kind of expression, in C++ 17 with leaving whole initializer constexpr
?
I'll try various options - passing by templated constructor, or reference to __data
- no way.
G++ says that casting from integer to pointer is not a constant expression. Ok, I know. But pointer arithmetic don't allow multiply, so even casting to unsigned char*
don't solve the problem.
So my question is: It is possible to calculate complex address in compile time in C++? According to bit-band address calculation of Cortex-M3 cores? It seems impossible in C++17 standard, but maybe someone know's the solution ( even only G++-accepted )