Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Variadic arguments in constructor of templated class

2 views
Skip to first unread message

John H.

unread,
Jun 12, 2009, 4:11:10 PM6/12/09
to
Using the following code on VS 6.0 and 2008:

#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?

Doug Harrison [MVP]

unread,
Jun 12, 2009, 4:45:24 PM6/12/09
to

ISTR it is undefined for the parameter just before the ellipsis to have
reference type.

--
Doug Harrison
Visual C++ MVP

John H.

unread,
Jun 12, 2009, 5:58:31 PM6/12/09
to
On Jun 12, 3:45 pm, "Doug Harrison [MVP]" <d...@mvps.org> wrote:
> ...

> ISTR it is undefined for the parameter just before the ellipsis to have
> reference type.

Yes, it appears that if I change the parameter to pass-by-value,
things work as expected.
Thank you for the insight.

0 new messages