reinterpret_cast is not a constant expression - needed help to find solution
Zygmunt Ptak
zygmuntptak@gmail.com
Sun Sep 3 20:20:00 GMT 2017
Hi,
I have a code for AVR written in c++.
To get the same size result as C code I'm using template classes.
For example:
#define AVR_REG_TO_U16(REG) (reinterpret_cast<u16>(& REG))
template<typename RegType, u16 reg_r>
struct Register
{
typedef Register<RegType, reg_r> Type;
constexpr Register<RegType, reg_r>() {}
static RegType & reg() { return *reinterpret_cast<RegType*>(reg_r); }
void operator = (RegType value) { reg() = value; }
void setBit(RegType value) { reg() = static_cast<RegType>(reg()
| 1 << value); }
void dropBit(RegType value) { reg() = static_cast<RegType>(reg()
& ~(1 << value)); }
void clear() { reg() = 0; }
void setBits(RegType mask) { reg() = static_cast<RegType>(reg()
| mask); }
void clearBits(RegType mask) { reg() = static_cast<RegType>(reg()
& ~mask); }
operator RegType() { return reg(); }
} __attribute__((packed));
// and finally:
Register<volatile uint8_t, AVR_REG_TO_U16(PIND)> pin_d;
pin_d.setBit(1);
Everything works pretty cool on old compiler.
But on newer (in my case gcc-7.1.0) there is an error:
error: 'reinterpret_cast<volatile uint8_t* {aka volatile unsigned
char*}>(41)' is not a constant expression
I tried found some solution, but nothing.
All the time the same problem.
Is it possible to turn off that option?
I understand that this type of the code doesn't make a sens on PC, but
on AVR it really does.
Of course that simple example looks like not having any benefit.
But it has, compiler nicely optimize code and as I said I'm able to
get the same size of the code like in pure C.
Size is really matter here with 2kB of the SRAM on atmega328p.
I don't want to use pure C. I want to use C++ which is better for my project.
Best regards
Zygmunt Ptak
More information about the Gcc-help
mailing list