Skip to main content
gamedev.net
🔒 Locked

Template function problem

Started by Wavarian Jun 26, 2004 at 3:57 AM 5 replies 1.1k views
Original Post
Wavarian
Wavarian
Greets, I'm having a little trouble with templated functions which is better explained with the following example:

template <typename T>
void foo(){

	cout << sizeof(T) << "\n";
}

void main(){

	foo<char>();
	foo<int>();
}
The output is always 4 bytes. If I remove
foo<int>();
the output is 1 byte. Why is "T" taking on the larger of the two, even if it is supposed to be representing a single char value? Thanks very much in advance.
SiCrane
SiCrane
I'm guessing broken compiler. Your code works fine under MSVC 7.1 and gcc 3.3.1.
DigitalDelusion
DigitalDelusion
Quote:
Original post by SiCrane
I'm guessing broken compiler. Your code works fine under MSVC 7.1 and gcc 3.3.1.


Im guessing VC6 here it's notoriously broken in the template department.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Wavarian
Wavarian
I didn't want to hear that =( but nevertheless, thanks alot guys =P I guess I'll just have to pass in the expected size as a parameter.

edit: and yes I'm using VC6 ><
risingdragon3
risingdragon3
Or you could specialize for most of the types. I assume you writing some input / output functions for binary mode file io?

void dostuff(int);
void dostuff(float);
void dostuff(char);
...
tedious, but blame vc6 :)
snk_kid
snk_kid
Quote:
Original post by wAVaRiaN
I didn't want to hear that =( but nevertheless, thanks alot guys =P I guess I'll just have to pass in the expected size as a parameter.

edit: and yes I'm using VC6 ><


You could try updating your VC++6 with SP5 then SP6, or you can download VC++ .NET 2003 compiler for free but doesn't come with VS IDE.
Wavarian
Wavarian
I might try that a little later snk_kid, hopefully that fixes the problem.

And thanks for the input RD3, however it seems far more feasible to simply pass in the size of T for the time being rather than copy and paste a large block of code several times, and then have to go through and debug each should something need to be changed - hence the existance of templates =PP

It works fine now, but I will be sure to check out those patches, probably huge in size no doubt >=|

Topic Locked

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

Sign in to reply to this topic.