I have been trying to figure out the problem that I am facing in the following piece of code.
#include <iostream>
#include <stdarg.h>
#include <vector>
using namespace std;
template <typename T>
class C {
vector<T> vec;
public:
void PrintValues(int size, T val, ...){
va_list v;
va_start(v,val);
for ( int i = 0 ; i < size ; i++ ) {
T p = va_arg(v,T);
vec.push_back(p);
cout<<p<<" ";
}
va_end(v);
}
};
int main() {
C<int> a;
C<char *> b;
cout<<endl;
a.PrintValues(10,1,4,6,2,8,5,3,7,10,9);
cout<<endl;
b.PrintValues(10,"a","b","c","d","e","f","g","h","i","j");
cout<<endl;
return 0;
}
I compiled this code on my Ubuntu 10.04 desktop using g++ 4.4.3.
The code compiled with the following warning
testcode.cpp: In function ‘int main()’:
testcode.cpp:25: warning: deprecated conversion from string constant to ‘char*’
While I was expecting an output like
1 4 6 2 8 5 3 7 10 9
a b c d e f g h i j
I actually got the following output
4 6 2 8 5 3 7 10 9 0
b c d e f g h i j `*@
Can someone please provide me with some pointers regarding why the first element in the list is skipped? I know that I can also do the same thing without using the variable argument list, but I am just trying out if this is possible.