template<class T> void ReportType()
{
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;
}
Out put is:
It''s a string!
It''s a string!
Wich Isn''t really what I expected. And if I switch the position of thoes functions they both report ints.
So... im speechless why an earth is it like that?
Is this how template functions is suposed to work?
Just when I though I understand templates this came up:
Visual Studio .NET 2003
program:
output:
It's a int!
It's a string!
[edited by - novum on July 8, 2003 12:21:08 PM]
program:
#include <iostream>template<class T> void ReportType(){ if(typeid(T) == typeid(std::string)) std::cout << "It's a string!" << std::endl; else if(typeid(T) == typeid(int)) std::cout << "It's a int!" << std::endl;}int main(){ ReportType<int>(); ReportType<std::string>(); return 0;}
output:
It's a int!
It's a string!
[edited by - novum on July 8, 2003 12:21:08 PM]
Visual Studio 6 has problems with template functions which do not take an argument of their templated type.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
quote:Original post by noVum
Visual Studio .NET 2003
program:
#include <iostream>template<class T> void ReportType(){ if(typeid(T) == typeid(std::string)) std::cout << "It''s a string!" << std::endl; else if(typeid(T) == typeid(int)) std::cout << "It''s a int!" << std::endl;}int main(){ ReportType<int>(); ReportType<std::string>(); return 0;}
output:
It''s a int!
It''s a string!
[edited by - novum on July 8, 2003 12:21:08 PM]
Im using visual studio c++ 6
And these are the includes:
#include <iostream>#include <string>using namespace std;
quote:Original post by Fruny
Visual Studio 6 has problems with template functions which do not take an argument of their templated type.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
So in other word''s what im trying to do isn''t bossible?
well it shouldn''t matter much, since when will you use a function such as the above with a template param but return type void and without arguments?
the real power of templates lies in generic programming, by letting the compiler deduce the types of the function.
take as an example this function:
now the beauty is, that you can use this function with any two values of a type that supports copying and operator*
thus
cout << mult(2,3) << endl << mult(0.12f, 34.56f) << endl;
works as well as does
Matrix A, B; ...
Matrix C = mult(A,B);
if Matrix has a copy ctor and operator*
the real power of templates lies in generic programming, by letting the compiler deduce the types of the function.
take as an example this function:
template < class T > T mult ( const T& t1, const T& t2 ){ return t1 * t2;}
now the beauty is, that you can use this function with any two values of a type that supports copying and operator*
thus
cout << mult(2,3) << endl << mult(0.12f, 34.56f) << endl;
works as well as does
Matrix A, B; ...
Matrix C = mult(A,B);
if Matrix has a copy ctor and operator*
I vaguely remember people adding a dummy, default parameter. Something like template<T> void Foo( T dummy = T() );, but I''m not sure, and it''s a horrible kludge anyway.
One thing I know, though, is that template function instances are treated just like ordinary function overloads, which may explain why it doesn''t quite work unless there is an argument of type ''T'' to actually differentiate between them.
Plus, as Burning_Ice points out, parameterless templated functions are not *that* useful.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
One thing I know, though, is that template function instances are treated just like ordinary function overloads, which may explain why it doesn''t quite work unless there is an argument of type ''T'' to actually differentiate between them.
Plus, as Burning_Ice points out, parameterless templated functions are not *that* useful.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
quote:Original post by Burning_Ice
well it shouldn't matter much, since when will you use a function such as the above with a template param but return type void and without arguments?
the real power of templates lies in generic programming, by letting the compiler deduce the types of the function.
take as an example this function:
template < class T > T mult ( const T& t1, const T& t2 ){ return t1 * t2;}
now the beauty is, that you can use this function with any two values of a type that supports copying and operator*
thus
cout
Thanks but I dare to say I know how templates work.
I just happend to need syntax:
Function<typename>();However would the broblem be fixed if I would't use <typename> whit call? like:template<class T> T tempfn(T t,int pos){// never use treturn *(T*)stack[pos];}int i; // useless var (my way to fix...)int rval = tempfn(i,0);now the type can be know but it's kinda dirty coding ain't it?
[edited by - Craazer on July 8, 2003 12:55:51 PM]
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:
void ReportType(const T& = T())
It''s a kludge but it works.
Not sure what it would be useful for (c:
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement