Varargs seem to lose argtype

Hi all - I’m not sure if this is an MbedOS problem or just a C++ problem. I’ve made a class to represent a PWM device (I’m using a PCA9685 PWM controller and an STM32-F446RE dev kit). For debugging purposes I added a utility method to print messages to the console serial port over the STLINK USB. This all works great. For example, I can write:

int size = snprintf(serialBuffer, SBUFFER_SIZE - 1, "[0x%02X] Sending servo %d %d %d\r\n", _i2caddr, channel, on, off);
serial->write(serialBuffer, size);

This outputs:

[0x80] Sending servo 3 0 450
[0x80] Sending servo 3 0 500

I decided to expose this as a function to print to the serial buffer as follows:

int Adafruit_PWMServoDriver::writeToSerial(const char * format, ... ) {
    if (serial) {
        va_list args;
        va_start(args, format);
        int size = snprintf(serialBuffer, SBUFFER_SIZE - 1, format, args);
        return serial->write(serialBuffer, size);
    return 0;

This works except for 1 major flaw - the actual variable values are completely borked. The variables are int[8|16]_t, and it seems like passing through the va_list they are being cast to integers and printing garbage.

[0x200013A0] Sending servo 38 536875936 536879264
[0x200013A0] Sending servo 38 536875936 536879264

Its not a big deal… its a convenience method for printing debug messages, but it is very odd. Is MBedOS not compiling the va_list part properly? Am I blind as a bat and can’t see something silly I’ve done?

Thanks for any input!



You should use a template parameter pack instead of va_list

see reference: Parameter pack(since C++11) -

and example:

Perfect, thank you so much, you rock! Sorry for posting what turned out to be a C++ question on MBed’s forums!