473,388 Members | 1,327 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

template and type

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
2 2013
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
5
by: Axter | last post by:
I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem. One...
1
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.