"mosfet" <tr******@wanadoo.fr> wrote...
I would like to write a template method, something like :
template <typename Type>
int MyClass::_Foo(vector<Type> MyVec)
Names that start with an underscore and a capital letter are
reserved by the implementation. It's better to drop that
habit.
{
if (int type)
....
if (long type)
....
return 0;
}
and be able to call it like this
vector<int> VecInt;
vector<long> VecLong;
_Foo(VecInt);
_Foo(VecLong);
But my question is how do I know inside the template what type is passed
(int or long)
The whole point of writing a template is to AVOID having to know what
type it's for. The different functionality has to be _extracted_ into
_different_ [overloaded] functions. The template should contain _only_
common code for ANY type. Otherwise, it's not a template any more.
Your problem can be solved in two forms:
template<class T> void FooT(vector<T>& v);
int Foo(vector<int>& v) {
... // some specific to 'int' functionality
FooT(v);
... // some more specific functionality
}
int Foo(vector<long>& v) {
... // some specific to 'long' functionality
FooT(v);
... // some more specific functionality
}
or
template<class T> int FooT(vector<T>& v) {
... // some common functionality
Foo(v);
... // some more common functionality
}
void Foo(vector<int>& v); // specific for 'int'
vpod Foo(vector<long>& v); // specific for 'long'
You need to decide which one suits you better.
Victor