I'm trying to count function arguments....
This code works okay:
#include <stdarg.h>
class Foo {
public:
template<typename ... Args>
Foo(Args const & ... args) {
Serial.println(sizeof...(Args));
}
};
void setup() {
Serial.begin(115200);
static Foo fooOne(1, 2, 3);
}
void loop() { }
/* OUTPUT IS: 3 */
But.... I'm trying to use it in this context so I do not need the leading argument that dictates the count.
#include <stdarg.h>
class Foo {
public:
byte size;
byte* arr;
Foo(byte size, ...) : size(size), arr(new byte [size]) {
va_list arguments;
va_start(arguments, size);
for (byte i = 0; i < size; i++)
arr[i] = va_arg(arguments, byte*);
va_end(arguments);
}
void print() {
for (byte i = 0; i < size; i++)
Serial.print((String)arr[i] + ", ");
Serial.println();
}
};
void setup() {
Serial.begin(115200);
static Foo fooOne(3, 1, 2, 3);
fooOne.print();
}
void loop() {
// put your main code here, to run repeatedly:
}
/* OUTPUT PRINTS THE ARRAY: 1, 2, 3, */
I'm basically trying to use example two, but infer the number of arguments like in example one.
If I try this:
#include <stdarg.h>
class Foo {
public:
byte size;
byte* arr;
template<typename ... Args>
Foo(Args const & ... args) : size(sizeof...(Args)), arr(new byte [sizeof...(Args)]) {
va_list arguments;
va_start(arguments, size);
for (byte i = 0; i < size; i++)
arr[i] = va_arg(arguments, byte*);
va_end(arguments);
}
void print() {
for (byte i = 0; i < size; i++)
Serial.print((String)arr[i] + ", ");
Serial.println();
}
};
void setup() {
Serial.begin(115200);
static Foo fooOne(1, 2, 3);
fooOne.print();
}
void loop() {
// put your main code here, to run repeatedly:
}
I get:
error: 'va_start' used in function with fixed args
va_start(arguments, size);
I feel the pieces are there, but I can't put it together (even after reading plenty of Google searches). I do think first the two examples above are using different things, yes? Parameter pack vs Variadic arguments or something like that?
Any helpers?