Sign in to follow this  
BeanDog

Crazy templates.

Recommended Posts

BeanDog    1065
I threw together a little templated multi-dimensional array class as an exercise. It won't compile, and I can't figure out why. I'm using GCC (g++).
/*
T is the type stored in the multi-dimensional array.
D is the depth of the array.
*/

template<class T, int D>
class multiarray
{	
private:
	multiarray<T,D-1>::pointer* m_pArrays;
	
public:
	alloc(int *pSizes)
	{
		m_pArrays = new multiarray<T,D-1>::pointer[*pSizes];
		for(int i = 0; i < *pSizes; i++)
			if(D > 1)
				m_pArrays[i].alloc(pSizes+1);
	}
	dealloc()
	{
		delete [] m_pArrays;
	}
	multiarray(int *pSizes)
	{
		alloc(pSizes);
	}
	~multiarray()
	{
		dealloc();
	}

	multiarray<T, D-1> &operator[](int idx)
	{
		return(m_pArrays[idx]);
	}

	//For D=2, this is a T**, D=3, this is a T***, etc.
	typedef multiarray<T,D-1>::pointer* pointer;
};

template<class T>
multiarray<T, 0>
{
public:
	typedef T pointer;
};
What's wrong here? ~BenDilts( void );

Share this post


Link to post
Share on other sites
rip-off    10976
im confused. what kind of array is this meant to represent.

maybe im just not following the code very well, but it

seems that

multiarray<int,10>

has a pointer to a

multiarray<int,9>

where are the ints stored?

why is it recursive. whats it supposed to store?

also


alloc(int *pSizes)
{


// THIS LINE HERE REQUIRES A DFEAULT CONSTRUCTOR, I THINK
m_pArrays = new multiarray<T,D-1>::pointer[*pSizes];



for(int i = 0; i < *pSizes; i++)

if(D > 1)

m_pArrays[i].alloc(pSizes+1);

}



other than that, what code are you using to instantiate the template?

what compiler errors do you get?

Share this post


Link to post
Share on other sites
Sharlin    864
Quote:


multiarray<T,D-1>::pointer



That should be

typename multiarray<T,D-1>::pointer

everywhere it occurs. The reason is that the compiler can't know whether a symbol like that which depends on template arguments (known as a dependent name) is a member variable or a type, and by default considers it the former, subsequently getting confused. Using the typename keyword here marks the name as a type.

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by rip-off
why is it recursive. whats it supposed to store?


He is trying to do an exercise in template metaprogramming, the code is suppose to make an multidimensional array of N dimensions where N is given at compile-time. The reason for the recursion is a multidimensional array can be viewed as an array of an array of an array .... of an array of T.

@BeanDog - as already mentioned you need to use typename to disambiguate type dependant names. Also your code has other issues first of all syntatic issues, some of your member functions are missing return types. Secondly when you do (partial) specializations you "generally" need to reimplement the entire specialization.

A tip when doing recursive metaprograms start with the special base case(s) first and then generalize. Also note that since the number of dimensions are known at compile-time you have the possibility to apply partial evaluation for most of the things you are going to do.

[Edited by - snk_kid on November 11, 2005 1:23:18 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this