Skip to content

EEPROM.length() should be static constexpr #351

Open
@awgrover

Description

@awgrover

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 ){

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions