0

I have a bunch of pins given names in the header:

#define DIP5 53
#define DIP4 52
#define DIP3 51
#define DIP2 50
#define DIP1 49
#define DIP0 48

If it's not obvious, I have a bunch of DIP switches connected to digital pins 48..53 on an Arduino Mega2560

In setup(), I need to set them all to INPUT_PULLUP. I know it would probably be less total effort to just do a series of pinMode commands, like:

pinMode(DIP5, INPUT_PULLUP);
pinMode(DIP4, INPUT_PULLUP);
// ... and so on

... but I'm trying to enrich my C++ skills & be fancy, so I'm trying to figure out how to declare an anonymous array and foreach through it... something like:

for (int pin : {DIP5, DIP4, DIP3, DIP2, DIP1, DIP0}) {
    pinMode(pin, INPUT_PULLUP);
}

... which, presumably, the compiler will optimize away into 6 inline pinMode commands anyway, but let me feel good about having pretty source code ;-)

except... when I try it, I get the following error:

error: deducing from brace-enclosed initializer list requires #include <initializer_list>

... but when I add #include <initializer_list>, I get a different error:

fatal error: initializer_list: No such file or directory

Any ideas what's wrong?

also... can I safely assume that the compiler will just unroll the whole thing behind the scenes, or is there a real risk it will actually declare a real array in SRAM just to iterate through one time?

2
  • define an array dip[0], dip[1] ....
    – jsotola
    Commented Jun 14, 2022 at 6:42
  • 1
    As for the "risk" of an array being created in SRAM, that would anyway be destroyed when the function in which it is defined, typically setup(), exits. If you explicitly define an array, you can still scan it with a C++11 range based for loop, which may improve the program's aesthetics.
    – 6v6gt
    Commented Jun 14, 2022 at 10:16

2 Answers 2

2

I'm trying to enrich my C++ skills & be fancy

The Arduino Mega is probably not the right platform to learn fancy C++. The development environment doesn't support all the modern C++ stuff. In particular...

fatal error: initializer_list: No such file or directory

...it rests on avr-libc, which is a C library with only limited support for C++.

can I safely assume that the compiler will just unroll the whole thing behind the scenes

No, you can't. The Arduino IDE, by default, calls avr-gcc with the -Os flag, which means “optimize for code size”. Loop unrolling is the kind of space vs. speed trade-off the compiler is unlikely to do with this optimization setting. You can disassemble the .elf file if you want to know for sure.

is there a real risk it will actually declare a real array in SRAM just to iterate through one time?

I suggest you follow jsotola's advice and declare the array yourself. You can declare the array PROGMEM if you want to avoid wasting RAM. But given that it need not take more than 6 bytes (as uint8_t[]), I would personally not even bother.

1

Edgar Bonet answers why for (int pin : {DIP5, DIP4, DIP3, DIP2, DIP1, DIP0}) { isn't working. What you would get (where supported) with this syntax is an initializer_list which itself is not an array type, although it is backed by an array.

If you really did want to do something like that with an AVR-based Arduino you could do:

for (int pin : (const int []){DIP5, DIP4, DIP3, DIP2, DIP1, DIP0}) {
    pinMode(pin, INPUT_PULLUP);
}

This is technically not standard C++. It is the use of something called a compound-literal, which is being used to make an array typed value. This is a feature of C language feature (from C99 onward), not C++. However, the compiler and its configuration for Arduino allows its use as a language extension.

Should you do this? Perhaps not, for the reasons that Edgar, 6v6gt, and jsotola point out. However, this much can be done if you really wanted. With respect to what it's doing efficiency-wise, you should test it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.