#include <cstdio>
#include <cstdarg>
template <class T, int SIZE>
class Point
{
public:
Point(T const & first, ...)
{
if(SIZE == 0)
{
return;
}
vals[0] = first;
va_list list;
va_start(list, first);
for(int i=1; i<SIZE; i++)
{
T const t = va_arg(list, T);
vals[i] = t;
}
va_end(list);
}
void print()
{
printf("(");
for(int i=0; i<SIZE; i++)
{
printf("%d", (int) vals[i]);
if(i+1<SIZE)
{
printf(", ");
}
}
printf(")");
}
private:
T vals[SIZE];
};
int main()
{
Point<int,3> p1(4, 2, -1);
p1.print();
return 0;
}
I get output (debug mode, although similar unexpected behavior in
release):
(4, 4, 4)
However using a newish version of g++ I get the expected (4, 2, -1).
Is this a bug in VC++ or am I doing something wrong?
ISTR it is undefined for the parameter just before the ellipsis to have
reference type.
--
Doug Harrison
Visual C++ MVP
Yes, it appears that if I change the parameter to pass-by-value,
things work as expected.
Thank you for the insight.