Const(expr) variables declared static in variant.h

Arduino Nani 33 IoT using Arduino IDE 2.0 / Eclipse IDE.

In variant.h the following constant expressions are declared as static. I assume this a mistake. I have not checked if the compiler is actually loading them into the relevant section (multiple times for each include presumably). In any case, as const or constexpr they don't appear to need static lifetime so the static qualifier should probably be removed unless this is some usage I am unaware of. If the latter then perhaps there is a better syntactic language construct to indicate the author's semantic intent.

static const uint8_t A0  = PIN_A0;
static const uint8_t A1  = PIN_A1;
static const uint8_t A2  = PIN_A2;
static const uint8_t A3  = PIN_A3;
static const uint8_t A4  = PIN_A4;
static const uint8_t A5  = PIN_A5;
static const uint8_t A6  = PIN_A6;
static const uint8_t A7  = PIN_A7;
static const uint8_t DAC0 = PIN_DAC0;

"static" used at the global level affects scope, rather than storage class.
"static const" at top level is the recommended replacement for #define of constants.

Recommended by ARM or Arduino or this specific compiler? It's not the recommended method of "replacing" macros in the C++ language. The C++ language equivalent would be constexpr or newer consteval as C++20. If I include a header with the statement
constexpr x = 123; then why would I need a static modifier? Every source file I include the header file in now has its own private little static variable. Seems odd to me. Oh well, if that's how they do it then fair enough.

As CONST's neither should actually generate memory, they're just symbols for the compiler and don't consume memory.

Precisely my point. Then why the static modifier? According the C++ reference there are three uses for the static keyword.

  1. declarations of namespace members with static storage duration and internal linkage

  2. definitions of block scope variables with static storage duration and initialized once

  3. declarations of class members not bound to specific instances

Our use case doesn't appear to fit any of those.

static scope would be the case except they are declared in a header file. Hence their scope is every file they are included in. If scoping was/is the issue that is best expressed using the language construct designed for scoping, namely namespace.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.