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!!! 8 1932
"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 myPreciousNewType {};
you want to add something to 'genericFun' to only do it to
an object of 'myPreciousNewType'. Here is how you do it:
// specialised:
template<> void genericFun<myPreciousNewType>(
myPreciousNewType& mpnt)
{
// basically copy all of it, then add some extra
}
or (better)
// overloaded:
void genericFun(myPreciousNewType& t)
{
// extra functionality
genericFun<myPreciousNewType>(t);
// extra functionality
}
YMMV, and variations of this are easily derived.
Victor
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:9jyzb.216281$Dw6.790779@attbi_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 myPreciousNewType {};
you want to add something to 'genericFun' to only do it to an object of 'myPreciousNewType'. Here is how you do it:
// specialised: template<> void genericFun<myPreciousNewType>( myPreciousNewType& mpnt) { // basically copy all of it, then add some extra }
or (better)
// overloaded: void genericFun(myPreciousNewType& t) { // extra functionality genericFun<myPreciousNewType>(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==type1 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!
"Eternally" <m@r.com> wrote in message
news:j%********************@twister.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)
"Jumbo" <pc****************@uko2.co.uk> wrote in message
news:10***************@news.minx.net.uk... "Eternally" <m@r.com> wrote in message news:j%********************@twister.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!
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 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.
"Eternally" <m@r.com> wrote in message
news:gX*********************@twister.nyroc.rr.com. .. "Victor Bazarov" <v.********@comAcast.net> wrote in message news:9jyzb.216281$Dw6.790779@attbi_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 myPreciousNewType {};
you want to add something to 'genericFun' to only do it to an object of 'myPreciousNewType'. Here is how you do it:
// specialised: template<> void genericFun<myPreciousNewType>( myPreciousNewType& mpnt) { // basically copy all of it, then add some extra }
or (better)
// overloaded: void genericFun(myPreciousNewType& t) { // extra functionality genericFun<myPreciousNewType>(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==type1 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.
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!!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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.
|
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...
|
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...
|
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...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |