You can do that using variadic arguments instead of messing with templates?
If you're allergic to messing with variadic arguments, you can alternatively use std::initializer_list (since C++11).
Unless you prefer templates and strings to lists within curly braces, or absolutely need to use strings at compile-time for a reason I don't know, this is easier, easier to understand and more elegant IMO. Example:
#include <cstdint>
#include <initializer_list>
#define PORT ((volatile uint32_t *) 0xABCD1234)
static void set_pin(uint8_t pin)
{
*PORT |= pin;
}
static void set_many_pins(std::initializer_list<uint8_t> pinList)
{
for (uint8_t pin : pinList)
{
set_pin(pin);
}
}
void Foo()
{
set_many_pins({ 1, 10, 8, 5, 2 });
}
(The set_pin() function is obviously hypothetical just to illustrate.)
This is what you get from the above for a Cortex-M4 at -O3:
Foo():
ldr r3, .L3
ldr r2, [r3, #564]
orr r2, r2, #1
str r2, [r3, #564]
ldr r2, [r3, #564]
orr r2, r2, #10
str r2, [r3, #564]
ldr r2, [r3, #564]
orr r2, r2, #8
str r2, [r3, #564]
ldr r2, [r3, #564]
orr r2, r2, #5
str r2, [r3, #564]
ldr r2, [r3, #564]
orr r2, r2, #2
str r2, [r3, #564]
bx lr
.L3:
.word -1412624384
Directly inlined.
At -Os, it uses an array and a loop instead:
Foo():
push {r4, lr}
ldr r1, .L4
ldr r0, .L4+4
movs r3, #6
.L2:
subs r3, r3, #1
bne .L3
pop {r4, pc}
.L3:
ldr r4, [r0, #564]
ldrb r2, [r1], #1 @ zero_extendqisi2
orrs r2, r2, r4
str r2, [r0, #564]
b .L2
.L4:
.word .LANCHOR0
.word -1412624384
.set .LANCHOR0,. + 0
C.0.0:
.byte 1
.byte 10
.byte 8
.byte 5
.byte 2