Is this how template functions is suposed to work?

Started by
13 comments, last by Craazer 20 years, 9 months ago
quote:Original post by petewood
As frunny says, change the function to:

void ReportType(const T& = T())

It's a kludge but it works.

Not sure what it would be useful for (c:


For this:

// used to call Pop<int>(); untill noticed problemtemplate<class T> void Pop(){ spos -= sizeof(T); delete (T*)svars[spos]; }// so im goin call it like this then:int t; // so type and size can be knownPop(t); 




[edited by - Craazer on July 8, 2003 1:01:23 PM]
Advertisement
What happens when you use

template <typename T>


instead of

template <class T>


Maybe it won''t help, but you can give it a go.

Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
quote:Original post by siaspete
What happens when you use

template <typename T>instead of    template <class T>


Maybe it won''t help, but you can give it a go.

Why you shouldn''t use iostream.h - ever! | A Good free online C++ book


It''s the same, thanks though.

Oh right, got it working now, thanks guys!
// kinda wierd stuff for me... but workstemplate<class T> void ReportType(const T& = T()) { if(typeid(T) == typeid(string))	 cout<<"It's a string! "<<endl; else if(typeid(T) == typeid(int))	 cout<<"It's a int! "<<endl;}int main(){ReportType<int>();ReportType<string>();return 1;}


[edited by - Craazer on July 8, 2003 1:07:06 PM]
Hmmm It seems that this:
template<class T> void ReportType(const T& = T())

doesn''t work inside classes becose T() cannot be found and so.

This topic is closed to new replies.

Advertisement