variable arg list issue with reference types
Hi,
I encountered a problem with variable-arg-list functions and MSVC.NET and
MSVC2005 compilers. It seems like having the starting variable (in va_start)
as a reference type messes up the values returned by va_arg().
See the enclosed code and output.
NB: this works fine with GCC 4.x.
Thanks, Cyril
------------------------------------------------------------------- CODE:
#include <string>
#include <iostream>
#include <stdarg.h>
using namespace std;
string print_int(const char *format, ...) {
va_list varg;
va_start(varg, format);
char res[1000]; sprintf_s(res, format, va_arg(varg, int));
va_end(varg);
return res;
}
string print_int(const string *format, ...) {
va_list varg;
va_start(varg, format);
char res[1000]; sprintf_s(res, format->c_str(), va_arg(varg, int));
va_end(varg);
return res;
}
string print_int(const string &format, ...) {
va_list varg;
va_start(varg, format);
char res[1000]; sprintf_s(res, format.c_str(), va_arg(varg, int));
va_end(varg);
return res;
}
int main() {
cout << print_int("overload print_int(const char*, ...) --> %d", 10) <<
endl;
string s1 = "overload print_int(const QString*, ...) --> %d";
cout << print_int(&s1, 10) << endl;
string s2("overload print_int(const QString&, ...) --> %d");
cout << print_int(s2, 10) << endl;
return 0;
}
------------------------------------------------------------------- OUTPUT:
overload print_int(const char*, ...) --> 10 // ok
overload print_int(const QString*, ...) --> 10 // ok
overload print_int(const QString&, ...) --> -858993460 // messed up