By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

template and type

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
mosfet wrote:
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.

Jul 19 '05 #2

P: n/a
"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

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.