Skip to main content
gamedev.net
🔒 Locked

[C++] Adding Template Parameters

Started by Mindowindow Jan 17, 2008 at 1:48 PM 4 replies 1.3k views
Original Post
Mindowindow
Mindowindow
This is what I'm trying to do:

template<WORD N, WORD M>
struct Matrix
{
    ...
    template<WORD P> 
    Matrix<N+P, M> append(const Matrix<P, M>&) const;
    ...
};

I get an 'INTERNAL COMPILER ERROR' when I use (instantiate) the templated method. However this works fine:

template<WORD N, WORD M>
struct Matrix
{
    ...
    Matrix<N+1, M> append(const Matrix<1, M>&) const;
    ...
};

I can place in any number 1, 2, 3...etc. for the rows to append but the compiler spills its guts out when I try to generalize the append function with the template P. What is the reason for this? Also the implimentation details are irrelevant, "empty" code still produces the same errors.
Zakwayda
Zakwayda
What compiler are you using?

Is there any more to the error message? If so, can you post it?

Also, can you post the code where you actually call the function?
Enigma
Enigma
typedef unsigned int WORD;template<WORD N, WORD M>struct Matrix{	template<WORD P> 	Matrix<N+P, M> append(const Matrix<P, M>&) const	{		return Matrix< N + P, M >();	}};int main(){	Matrix< 3, 3 > m1;	Matrix< 1, 3 > m2;	m1.append(m2);}

Compiles fine under Borland 5.69, GCC 3.4.2 and MSVC 2008.

Σnigma
Mindowindow
Mindowindow
@jyk - I'm using MSVC++ 2003, sorry, I should have specified.

@Enigma - Actually, I pasted the barebones code you tried out into a new project and it compiled fine on MSVC++ 2003.

After a good deal of guess and check I have found the problem.

Enigma, you typed (and I copy/pasted your code, fortunately)
typedef unsigned int WORD;


(where its defined in my project) defines it as
typedef unsigned short WORD;


If I use
template<unsigned int/*or long*/ P>

the program compiles and lives happily ever after.

If P is an unsigned short, the compiler keels over and dies. Can someone offer an explination?
Antheus
Antheus
Quote:
If P is an unsigned short, the compiler keels over and dies. Can someone offer an explination?


WORD is a Windows API type.

Don't use it.

As a matter of fact, don't use ALLCAPS for types in C++.

Common primitive types of specific size, while not formally defined (you can use boost though), have the form of TYPE_t or uTYPE_t, such as ushort16_t, int32_t or ulong64_t.

For templates meta-programming of this type, it's common to use primtiive types to reduce dependencies:
template <unsigned int M, unsigned int N> struct Matrix;


Templates are fully specialized during compile-time - there is no different which type you use, they don't consume any memory, so using a short isn't saving you anything.
Mindowindow
Mindowindow
I've always used WORD and DWORD for reasons that they are short and compact, and have sensible meanings to me. I suppose I could make my own typedefs as commonly outlined, but I don't see the need too. I'm not writing portable code at the moment, and even if so, changing all the WORDs in a project is not that difficult. Thats a matter of preference/style/circumstance I should think.

The issue is why would an unsigned short would screw up this template for a compiler? I understand that making the parameter an unigned short has no effect on the end compiled code. That is exactly the point. I used WORD in the template declaration to suggest that you should not create a matrix with very large dimensions, as that would probably cause stack overflow. This is not a very good reason of course, but when I decided on the template parameter type I assumed it would make no difference to the actual compilation, so I just WORD to hint at feasible usage.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.