Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7952 Discussions

Problems with variable list of arguments

sdrajan
Beginner
451 Views
I have created a simple program to replicate a problem I am having. I am using Intel Windows C++ compiler Version 7.1 with Windows 2000. The following program prints expected results when int and double are used but not when float values are used.

#include
#include
#include

template
void VarTypes (int nV, T v, ...)
{
va_list marker;

va_start (marker, nV);
for (int i=1; i <= nV; i++)
{
T value = va_arg (marker, T);
std::cout << "Value: " << value << ' ';
}
va_end (marker);
}

int main ()
{
int n1=1, n2=2, n3=3;
float f1=1.23f, f2=2.34f, f3=3.45f;
double d1=1.23, d2=2.34, d3=3.34;

VarTypes (3, n1, n2, n3); std::cout << ' ';
VarTypes (3, f1, f2, f3); std::cout << ' ';
VarTypes (3, d1, d2, d3); std::cout << ' ';

return 0;
}

Any ideas?

Thanks.

Rajan
0 Kudos
3 Replies
TimP
Honored Contributor III
451 Views
Pay attention to the diagnostics you will get if you build this with g++. Apparently, the compilers don't find their way around the contradiction between your promotion to double, forced by the use of the varargs facility, and your declaration of type float. This must be a common error, since g++ is so kind as to explain it.
0 Kudos
cp_jain
Beginner
451 Views
Hi Rajan,
This is a not the issue with compiler.

In the variable-length part of a variable-length argument list, the "default argument promotions" apply: types char and short int are promoted to int, and float is promoted to double. [ANSI Standard]

Please refer to
http://vision.eng.shu.ac.uk/C++/c/c-faq/cfaq15.html

to get more details on this.

CP
0 Kudos
sdrajan
Beginner
451 Views
It would nice if the Intel C++ Windows compiler (with the default compiler options) would print out diagnostics as well as the g++ compiler!

Thanks.
0 Kudos
Reply