-
templates in a class
How would I make a template function for a class? (I mean a member function that is a template. Not an entire template class) Im getting parse errors from having the 'template <typename T>' thing above the prototype and member function....
Prototype and member function code please
:)
-
Hi,
Stroustrup prefers 'class' over 'typename' because it is shorter and quicker to type. See "The C++ Programmers Language", Third Edition, page 858 to read it for yourself.
I read from one source that 'typename' was not supported by all compilers. I don't understand nor can I explain this.
Code:
#include <cstdlib>
#include <iostream>
template <class T>
T mymin(T v1, T v2)
{
return ((v1<v2)?v1:v2);
}
int main(int argc, char *argv[])
{
int i=123;
int j=456;
int min= mymin(i,j);
cout << min << endl;
system("pause"); //I use Dev-C++...need this here.
return 0;
}
Regards,
ChuckB
-
Chuck:
typename was introduced only later, as class might be confusing to newbies. It is never a good thing to have one keyword mean two different thigns in different contextes.
Your fingers won't fall off from typing 3 characters more. Neither will Bjarne's. :rolleyes:
Since typename wasn't there from the beginning some older compilers don't support it.
As to you code, I think he asked for member template functions. I don't see why this shouldn't work:
Code:
class A {
public:
template <typename/class T>
void SomeFunc(T arg);
};
template <class/typename T>
void A::SomeFunc(T arg) {
// blabla
}
-
CornedBee,
I'm still trying to get this terminology down. I understand now the expression 'member function'. I do like the 'class/typename' usage. I haven't tried that.
I admit, I was confused using the word class in my function example.
You know, I'm just glad I have a clue what this thread was saying. :-)
Regards,
ChuckB