Archived

This topic is now archived and is closed to further replies.

Is this how template functions is suposed to work?

This topic is 5270 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just when I though I understand templates this came up:
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 << 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*

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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 t

return *(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]

Share this post


Link to post
Share on other sites
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 problem

template<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 known

Pop(t);




[edited by - Craazer on July 8, 2003 1:01:23 PM]

Share this post


Link to post
Share on other sites
Oh right, got it working now, thanks guys!

// kinda wierd stuff for me... but works

template<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]

Share this post


Link to post
Share on other sites