-
-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error: a reinterpret_cast is not a constant expression #36
Comments
I was just running into this same error compiling DSMR on a regular PC instead of for Arduino. Good to know this also occurs with certain ESP cores. The problem is probably that newer gcc versions (correctly) check the constness of expressions per the standard, where older versions would (incorrectly) allow this expression. I tried the following patch, which seems to compile (but I haven't managed linking successfully, so I'm not sure if it actually works). If you try it, let me know if it works for you: --- a/src/dsmr/fields.h
+++ b/src/dsmr/fields.h
@@ -176,13 +176,19 @@ const uint8_t WATER_MBUS_ID = 2;
const uint8_t THERMAL_MBUS_ID = 3;
const uint8_t SLAVE_MBUS_ID = 4;
+template <typename FieldT>
+struct NameConverter {
+ public:
+ operator const __FlashStringHelper*() const { return reinterpret_cast<const __FlashStringHelper*>(&FieldT::name_progmem); }
+};
+
#define DEFINE_FIELD(fieldname, value_t, obis, field_t, field_args...) \
struct fieldname : field_t<fieldname, ##field_args> { \
value_t fieldname; \
bool fieldname ## _present = false; \
static constexpr ObisId id = obis; \
static constexpr char name_progmem[] DSMR_PROGMEM = #fieldname; \
- static constexpr const __FlashStringHelper *name = reinterpret_cast<const __FlashStringHelper*>(&name_progmem); \
+ static constexpr NameConverter<dsmr::fields::fieldname> name = {}; \
value_t& val() { return fieldname; } \
bool& present() { return fieldname ## _present; } \
} I'll probably continue work on this in the coming days, hopefully I can push a fix out soon. |
Thanks for the quick response. I was already hitting my head at the wall, wondering what i did wrong with my build environment, but indeed good to hear that it's indeed intentionally failing. For me, it still fails compiling, although it finds a conflicting declaration.
|
Ah, I see I missed one. Try removing this line from fields.cpp:
If that still breaks, maybe replace it with:
All of the other |
Ah, that first one did the trick. |
Yep, parsing seems to work perfectly with the patch you mentioned, plus removing the |
Awesome, thanks for confirming. I'll try to push out this change to the master branch in the coming days. |
Applied patch from: matthijskooijman#36 (comment)
Apply patch from: matthijskooijman#36 (comment)
Newer gcc versions correctly enforce a C++ language limitation that forbids using reinterpret_cast in constant expressions. This commit works around this by replacing the `name` attribute with a `get_name()` method that delays the cast until the time of use. Note that the method will be inlined in practice, so there is no additional overhead. For compatibility with existing code, the name attribute is replaced by a NameConverter object that can be implicitly converted to `const __FlashStringHelper *`. This should keep some code working, but not everything (e.g. conversion to String can no longer be done implicitly, see matthijskooijman#43). In the future, the name attribute should probably be completely removed, and replaced by a `name()` method (which is a bit more elegant than the current `get_name()`) This fixes matthijskooijman#36.
Hi,
I'm been changing from an ESP32/Arduino environment into an ESP32/esp-idf environment that has support for Arduino-code, and so far everything has been smooth, except for this library.
Somehow, the compiler trips over the following code:
at the reinterpret_cast<.... part it gives me this error:
What i've tried/checked
Could you give me any hints where i should look to solve this issue?
The text was updated successfully, but these errors were encountered: