473,624 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you know datatype when using Templates?

Hi folks,

I've got a program which has a function which uses templates to accept
parameters of any type.

Works well, but there's one certain datatype which I want to special case
and do an extra thing to. The datatype is a class I made.

Is there anyway for me to test a parameters datatype in a template using
function?

Thanks a lot!!!
Jul 22 '05 #1
8 1942
"Eternally" <m@r.com> wrote...
I've got a program which has a function which uses templates to accept
parameters of any type.

Works well, but there's one certain datatype which I want to special case
and do an extra thing to. The datatype is a class I made.

Is there anyway for me to test a parameters datatype in a template using
function?


No. Well, there may be, but you better forget about it. That
is not the C++ way. The C++ way would be to either specialise
your template based on your new type or to overload the function
based on your type.

So, given

template<class T> void genericFun(T& t)
{
// does something to 't'
}

class myPreciousNewTy pe {};

you want to add something to 'genericFun' to only do it to
an object of 'myPreciousNewT ype'. Here is how you do it:

// specialised:
template<> void genericFun<myPr eciousNewType>(
myPreciousNewTy pe& mpnt)
{
// basically copy all of it, then add some extra
}

or (better)

// overloaded:
void genericFun(myPr eciousNewType& t)
{
// extra functionality
genericFun<myPr eciousNewType>( t);
// extra functionality
}

YMMV, and variations of this are easily derived.

Victor
Jul 22 '05 #2

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:9jyzb.2162 81$Dw6.790779@a ttbi_s02...
"Eternally" <m@r.com> wrote...
I've got a program which has a function which uses templates to accept
parameters of any type.

Works well, but there's one certain datatype which I want to special case and do an extra thing to. The datatype is a class I made.

Is there anyway for me to test a parameters datatype in a template using
function?


No. Well, there may be, but you better forget about it. That
is not the C++ way. The C++ way would be to either specialise
your template based on your new type or to overload the function
based on your type.

So, given

template<class T> void genericFun(T& t)
{
// does something to 't'
}

class myPreciousNewTy pe {};

you want to add something to 'genericFun' to only do it to
an object of 'myPreciousNewT ype'. Here is how you do it:

// specialised:
template<> void genericFun<myPr eciousNewType>(
myPreciousNewTy pe& mpnt)
{
// basically copy all of it, then add some extra
}

or (better)

// overloaded:
void genericFun(myPr eciousNewType& t)
{
// extra functionality
genericFun<myPr eciousNewType>( t);
// extra functionality
}

YMMV, and variations of this are easily derived.

Victor


Ohhhhh....Rats! :)

Thanks for the help. I was trying to avoid that, but I guess that's the way
to go.

I say "Rats!" because I kind of accidentally fibbed. Right now I only need
to special case one datatype, but soon it'll be like 5. They'll all be
special cased in the same exact manner though, so the psuedocode could've
looked like:

if(datatype==ty pe1 or datatype==type2 or datatype==type3 or.....){
do the special case
}

I would just create 2 seperate functions with different names, but the
function that we're talking about here is actually the constructor function
in a class that I have.

Thanks a lot for the help though. If you can think of anything else, please
respond, but otherwise I'll just overload it X number of times.

Thanks again!
Jul 22 '05 #3

"Eternally" <m@r.com> wrote in message
news:j%******** ************@tw ister.nyroc.rr. com...
Hi folks,

I've got a program which has a function which uses templates to accept
parameters of any type.

Works well, but there's one certain datatype which I want to special case
and do an extra thing to. The datatype is a class I made.

Is there anyway for me to test a parameters datatype in a template using
function?

Yes I think I know what you mean , there is a STL <typeinfo>
It works something like this:

#include <iostream>
#include <typeinfo>

class UDT{};

template <typename T>
void foo(T arg){
std::cout << typeid(T).name( ) << '\n';
}

int main(){
int intX =0;
char chX =0;
UDT udtX;

foo(intX), foo(chX), foo(udtX);
return 0;
}

This will output the following:
int
char
class UDT
....

Look up your docs to get more info about it and you might need to set
compiler options to enable runtime type info .
HTH.
:o)
Jul 22 '05 #4

"Jumbo" <pc************ ****@uko2.co.uk > wrote in message
news:10******** *******@news.mi nx.net.uk...

"Eternally" <m@r.com> wrote in message
news:j%******** ************@tw ister.nyroc.rr. com...
Hi folks,

I've got a program which has a function which uses templates to accept
parameters of any type.

Works well, but there's one certain datatype which I want to special case and do an extra thing to. The datatype is a class I made.

Is there anyway for me to test a parameters datatype in a template using
function?

Yes I think I know what you mean , there is a STL <typeinfo>
It works something like this:

#include <iostream>
#include <typeinfo>

class UDT{};

template <typename T>
void foo(T arg){
std::cout << typeid(T).name( ) << '\n';
}

int main(){
int intX =0;
char chX =0;
UDT udtX;

foo(intX), foo(chX), foo(udtX);
return 0;
}

This will output the following:
int
char
class UDT
...

Ahhh.....That's perfect. Exactly what I need. Just tested it and it works
great.

Thanks!
Jul 22 '05 #5
On Thu, 04 Dec 2003 06:40:29 GMT, "Eternally" <m@r.com> wrote:
Yes I think I know what you mean , there is a STL <typeinfo>
It works something like this:

#include <iostream>
#include <typeinfo>

class UDT{};

template <typename T>
void foo(T arg){
std::cout << typeid(T).name( ) << '\n';
}

int main(){
int intX =0;
char chX =0;
UDT udtX;

foo(intX), foo(chX), foo(udtX);
return 0;
}

This will output the following:
int
char
class UDT
...

Ahhh.....That' s perfect. Exactly what I need. Just tested it and it works
great.


Note that typeid(T) is a relatively slow operator, whereas template
specialization is compile time and therefore has zero time overhead.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6

Ahhh.....That' s perfect. Exactly what I need. Just tested it and it works
great.

Thanks!


It may work, but it slows down your application.

What's wrong with template specialization/overload as Victor
suggested, anyways? Instead of checking type ID and doing a manual
switch to select a function, you let the compiler generate that code
for you. MUCH simpler! And faster. And cleaner.
Jul 22 '05 #7

"Eternally" <m@r.com> wrote in message
news:gX******** *************@t wister.nyroc.rr .com...

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:9jyzb.2162 81$Dw6.790779@a ttbi_s02...
"Eternally" <m@r.com> wrote...
I've got a program which has a function which uses templates to accept
parameters of any type.

Works well, but there's one certain datatype which I want to special case and do an extra thing to. The datatype is a class I made.

Is there anyway for me to test a parameters datatype in a template using function?
No. Well, there may be, but you better forget about it. That
is not the C++ way. The C++ way would be to either specialise
your template based on your new type or to overload the function
based on your type.

So, given

template<class T> void genericFun(T& t)
{
// does something to 't'
}

class myPreciousNewTy pe {};

you want to add something to 'genericFun' to only do it to
an object of 'myPreciousNewT ype'. Here is how you do it:

// specialised:
template<> void genericFun<myPr eciousNewType>(
myPreciousNewTy pe& mpnt)
{
// basically copy all of it, then add some extra
}

or (better)

// overloaded:
void genericFun(myPr eciousNewType& t)
{
// extra functionality
genericFun<myPr eciousNewType>( t);
// extra functionality
}

YMMV, and variations of this are easily derived.

Victor


Ohhhhh....Rats! :)

Thanks for the help. I was trying to avoid that, but I guess that's the

way to go.

I say "Rats!" because I kind of accidentally fibbed. Right now I only need to special case one datatype, but soon it'll be like 5. They'll all be
special cased in the same exact manner though, so the psuedocode could've
looked like:

if(datatype==ty pe1 or datatype==type2 or datatype==type3 or.....){
do the special case
}

I would just create 2 seperate functions with different names, but the
function that we're talking about here is actually the constructor function in a class that I have.

Thanks a lot for the help though. If you can think of anything else, please respond, but otherwise I'll just overload it X number of times.


You can specialize constructors just incase you don't realise this.
So you have a different constructor for each specialized type.

This is probably the more efficient way but you'll know best exactly what
your trying do.

HTH.
Jul 22 '05 #8
Hi,

You can use type_info class to get the type information inside a template
method.

One more way is CRuntimeClass if you are using MFC. But is it good to use
specific type stuff inside a template method ?

Bye
Chandra

Eternally wrote:
Hi folks,

I've got a program which has a function which uses templates to accept
parameters of any type.

Works well, but there's one certain datatype which I want to special case
and do an extra thing to. The datatype is a class I made.

Is there anyway for me to test a parameters datatype in a template using
function?

Thanks a lot!!!


Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
13429
by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as strings) at least 12 digits (characters) long - all positions will be occupied (no leading 0's) - Tables may have upto 1m+ rows
1
1341
by: Foehammer | last post by:
Hello, On our corporate website, we will be using email notifications for various things. I would like for our marketing guy to be able to edit the email templates over the web so that I don't have to keep updating them myself for every little change. I want to store the templates within the database (since they will be small). The templates will be HTML based and not more than a few thousand bytes in length. Should I use a VARCHAR or...
4
1700
by: c_bowen | last post by:
I am using Visual Studio .NET 2003 with SQL Server 2000. I am trying to insert the date and time into a SQL database by using hour(now). I am having a hard time trying to figure out which datatype to use in SQL to store this value. I have tried using datetime, char, nchar, text and nothing seems to work. Anyone have any ideas? Thanks! Regards, :) Christopher Bowen
0
1681
by: SoYouKnowBrig | last post by:
Hi All, I am using Microsoft.ApplicationBlocks.Cache.CacheManager to persist a System.Data.Dataset object. This Dataset object has a DataTable that is created from an existing DataTable using the Clone() method. Before I add the new DataTable to the DataSet, I change the DataType of a DataColumn from System.String to System.Int64. I then add data to the new table and then add it to the DataSet. Then DataSet is then added to the Cache....
6
2156
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows XP Pro SP 1, .Net Framework 1.1) and on our production server (Windows 2K SP 4, .Net Framework 1.1). I have simplified the code and data to isolate the problem. When I use the xsl:strip-space (Line 12) declaration in conjunction with the xsl:sort...
2
1888
by: David Slinn | last post by:
I have created a simple XML document, generated a schema from it (which Visual Studio makes everything a string element). I then went through and changed all fields that are dates to the Date data type. Now, when I use the datagrid to edit the data in the XML file and then switch back to the raw XML text date, the dates are formatted like this "1973-10-27T00:00:00.0000000-06:00" and not as I expected, which are supposed to be a simple...
2
2329
by: intrepidca | last post by:
When I try to translate an XML file (using org.apache.xalan.xslt.Process) that has a DOCTYPE declaration, I only get the <?xml ...?> processing instruction in the output file. I get no error messages. If I remove the DOCTYPE declaration it works fine. I have checked that the XML file is valid according to the DTD (using xmllint) and that checks out. Here are snippets from the XML and the XSL files and debugging output from xalan: the...
9
1947
by: manish | last post by:
we have avariablr int bps; // bps determined during run time - - - how can we make conditional declaration of a variable y we want appropriate declaration depending upon bps.
11
1882
by: BD | last post by:
Hi, all. I'm running 8.2 on Windows. This is a development platform for a project whose production environment is running on a mainframe. I believe that the RI compilation process is not quite as robust on Windows as it is in other environments. Here's what I'm finding:
0
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8619
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.