template and type

M

mosfet

Hi

I would like to write a template method, something like :

template <typename Type>
int MyClass::_Foo(vector<Type> MyVec)
{
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)

Thanks
 
G

Gianni Mariani

mosfet said:
Hi

I would like to write a template method, something like :

template <typename Type>
int MyClass::_Foo(vector<Type> MyVec)
{
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)


You can use specializations e.g.


template <typename Type>
void stuff_by_type( ... )
{
}

template <>
void stuff_by_type<int>( ... )
{
.... int stuff ...
}

template <>
void stuff_by_type<float>( ... )
{
.... float stuff ...
}


template <typename Type>
int MyClass::_Foo(vector<Type> MyVec)
{
...

stuff_by_type<Type>( ... );

...

return 0;
}


You theoretically could use ...


template <typename Type>
int MyClass::_Foo(vector<Type> MyVec)
{
if (typeid(Type) == typeid(int))
.....
if (typeid(Type) == typeid(float))
.....

return 0;
}

Almost allways you end up using some nasty typecasts in the if blocks
because of invalid conversions - not good.
 
V

Victor Bazarov

mosfet said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top