How do you know datatype when using Templates?

Discussion in 'C++' started by Eternally, Dec 4, 2003.

  1. Eternally

    Eternally Guest

    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!!!
     
    Eternally, Dec 4, 2003
    #1
    1. Advertising

  2. "Eternally" <> 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, Dec 4, 2003
    #2
    1. Advertising

  3. Eternally

    Jumbo Guest

    "Eternally" <> wrote in message
    news:j%xzb.185013$...
    > 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.
    :eek:)
     
    Jumbo, Dec 4, 2003
    #3
  4. Eternally

    Eternally Guest

    "Victor Bazarov" <> wrote in message
    news:9jyzb.216281$Dw6.790779@attbi_s02...
    > "Eternally" <> 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, Dec 4, 2003
    #4
  5. Eternally

    Eternally Guest

    "Jumbo" <> wrote in message
    news:...
    >
    > "Eternally" <> wrote in message
    > news:j%xzb.185013$...
    > > 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!
     
    Eternally, Dec 4, 2003
    #5
  6. Eternally

    tom_usenet Guest

    On Thu, 04 Dec 2003 06:40:29 GMT, "Eternally" <> 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
     
    tom_usenet, Dec 4, 2003
    #6
  7. Eternally

    Dan W. Guest


    >
    >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.
     
    Dan W., Dec 4, 2003
    #7
  8. Eternally

    Jumbo Guest

    "Eternally" <> wrote in message
    news:gXzzb.185871$...
    >
    > "Victor Bazarov" <> wrote in message
    > news:9jyzb.216281$Dw6.790779@attbi_s02...
    > > "Eternally" <> 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.
     
    Jumbo, Dec 4, 2003
    #8
  9. 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!!!
     
    chandra sekhar, Dec 5, 2003
    #9
  10. Eternally

    mubassara

    Joined:
    Aug 21, 2008
    Messages:
    1
    Could u plz tel me the compiler setting to enable runtime type info .
     
    mubassara, Aug 21, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    521
  2. recover
    Replies:
    2
    Views:
    857
    recover
    Jul 25, 2006
  3. Jeff
    Replies:
    2
    Views:
    1,197
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Apr 16, 2007
  4. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    276
    Gregory Toomey
    Apr 23, 2004
  5. Googy
    Replies:
    3
    Views:
    120
    -berlin.de
    Jan 15, 2007
Loading...

Share This Page