This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

g++ 3.1 bug in optimization of ellipsis


hi all,

in the attached source I use the ellipsis to pass a variable number of 
arguments to the constructor of a N-dimensional vector. When I compile 
this source with optimization O1 or O2, I obtain an error:

test.cc: In function `int main()':
test.cc:24: `va_start' used in function with fixed args

when I use any other optimization (or no optimization at all) it is 
compiled correctly. Using gcc-3.0.4 this problem doesn't appear.

thanks to anybody.
bye,
andrea

#include <iostream>
#include <cstdarg>
#include <cstddef>
#include <cmath>

template <class Type> class VectorNd {

	size_t size;

	Type *data;

public:

	VectorNd(const VectorNd &v ) : size(v.size) { data = new Type[size]; for (size_t i = 0; i < size; i++) data[i] = v.data[i]; }
	VectorNd(size_t _size, Type *_data ) : size(_size) { data = new Type[size]; for (size_t i = 0; i < size; i++) data[i] = _data[i]; }
	VectorNd(size_t _size, Type arg ) : size(_size) { data = new Type[size]; for (size_t i = 0; i < size; i++) data[i] = arg; }
	VectorNd(size_t _size = 1 ) : size(_size) { data = new Type[size]; }
	VectorNd(size_t _size, size_t count, ... ) : size(_size)
	{
		data = new Type[size]; 
	
		va_list ap;

		va_start(ap, count);

		for (size_t i = 0; i < count; i++) data[i] = va_arg(ap, Type);

		va_end(ap);
	}	
	
	~VectorNd() { delete []data; }

	const VectorNd &operator = (const VectorNd &v )
	{
		if (this != &v)
		{
			if (size != v.size)
			{
				delete []data;
			
				data = new Type[size = v.size];
			}
			
			for (size_t i = 0; i < size; i++) data[i] = v.data[i]; 
		}

		return *this;
	}
	
	friend Type abs(const VectorNd &v )
	{
		Type sum = v.data[0] * v.data[0];
		
		for (size_t i = 1; i < v.size; i++)	sum += v.data[i] * v.data[i];
	
		return sqrt(sum);
	}

	friend std::ostream &operator << (std::ostream &stream, const VectorNd &v )	
	{ 
		stream << v.data[0];
		
		for (size_t i = 1; i < v.size; i++) stream << ", " << v.data[i]; 
		
		return stream; 
	}

};

int main()
{
	VectorNd<double> vector;
	
	vector = VectorNd<double>(	3, 	// number of components
								3, 	// number of components passed through "..." (must be <= of the previous parameter)
								1.0, 2.0, 3.0);

	std::cout << "abs(" << vector << ") = " << abs(vector) << std::endl;

	return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]