arduino / arduino/ArduinoCore-avr

EEPROM.length() should be static constexpr

Open
#351 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Couldn't `EEPROM.length()` be static constexpr? I wanted to use `.length()` in a class static, like:
```
class EEPROM_User {
// works even if actual eeprom < 3k, i.e. wrap-around
constexpr static int ADDR = (3169) % EEPROM.length();
}
```

In Arduino IDE 1.8.12, `./hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h`:
```
uint16_t length() { return E2END + 1; }
```
And E2END is compile-time static.

I propose:
```
static constexpr uint16_t length() { return E2END + 1; }
```

Patch:
```
--- a/hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h 2020-06-21 17:46:29.124231608 -0400
+++ b/hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h 2020-06-21 17:56:01.608338869 -0400
@@ -124,7 +124,7 @@
//STL and C++11 iteration capability.
EEPtr begin() { return 0x00; }
EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
- uint16_t length() { return E2END + 1; }
+ static constexpr uint16_t length() { return E2END + 1; }

//Functionality to 'get' and 'put' objects to and from EEPROM.
template< typename T > T &get( int idx, T &t ){
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Review hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h, focusing on EEPROM::length() and its use by end(). Check whether making length() static constexpr supports the shown class-static address calculation without breaking existing calls. Done means the API change is compatible and the requested compile-time use works.

Written by the indexing model from the issue text.

Assessment

Tech stack
arduino, cpp
Domain
embedded-iot
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.