Template function: Can it tell if parameter is primitive or class?

Discussion in 'C++' started by kk_oop@yahoo.com, Jul 18, 2008.

  1. Guest

    In C++, is there a way for a template function to determine at runtime
    if its template parameter is a primitive type vs. a class?

    I want to do something like this:

    template<typename T>
    void doThis( T )
    {
    if (T is a primitive type)
    {
    doOneThing();
    }
    else //it's a class
    {
    doSomethingElse( );
    }

    }

    Thanks!

    Ken
    , Jul 18, 2008
    #1
    1. Advertising

  2. Jerry Coffin Guest

    In article <99048a55-20b7-4a25-9601-83a708efe286
    @f63g2000hsf.googlegroups.com>, says...
    > In C++, is there a way for a template function to determine at runtime
    > if its template parameter is a primitive type vs. a class?


    A fundamental rule with templates is that everything happens at compile
    time, not run time.

    > I want to do something like this:
    >
    > template<typename T>
    > void doThis( T )
    > {
    > if (T is a primitive type)
    > {
    > doOneThing();
    > }
    > else //it's a class
    > {
    > doSomethingElse( );
    > }
    >
    > }


    I hope not -- one of the fundamental ideas with templates is that (as
    much as possible) you get duck typing -- i.e. anything that can _act_ in
    the right ways IS the right kind of thing. If I create an Integer class
    that supports all the same operations as an int (for example), it should
    be treated like an int.

    The real questions (I think) would be why you think you want to do
    anything like this -- what _characteristics_ of built-in types are
    important, and why do you want to distinguish them from user defined
    types? Chances are pretty fair that if you can define those
    characteristics, you can also make use of them to provide the
    distinction you really care about.

    For example, if you care about the fact that you can't derive from a
    built-in type, and want to treat potential base classes differently from
    things that can't be derived from, that's a possibility that could at
    least be explored and an answer might be found.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jul 18, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    Re: Template function: Can it tell if parameter is primitive orclass?

    On Jul 18, 11:35 pm, wrote:
    > In C++, is there a way for a template function to determine at
    > runtime if its template parameter is a primitive type vs. a
    > class?


    There are no templates at runtime; templates are fully resolved
    into functions or classes at compile time.

    > I want to do something like this:


    > template<typename T>
    > void doThis( T )
    > {
    > if (T is a primitive type)
    > {
    > doOneThing();
    > }
    > else //it's a class
    > {
    > doSomethingElse( );
    > }
    > }


    Those aren't the only two possibilities, but see "C++ Templates:
    The Complete Guide", by Vandevoorde and Josuttis. All of
    chapter 19 deals with type classification, and there is an
    earlier section, 15.2.2 which treats determining class types.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 19, 2008
    #3
  4. Guest

    Re: Template function: Can it tell if parameter is primitive orclass?

    On Jul 19, 5:35 am, wrote:
    > In C++, is there a way for a template function to determine at runtime
    > if its template parameter is a primitive type vs. a class?
    >
    > I want to do something like this:
    >
    > template<typename T>
    > void doThis( T )
    > {
    >   if (T is a primitive type)
    >   {
    >      doOneThing();
    >   }
    >  else //it's a class
    >   {
    >     doSomethingElse( );
    >   }
    >
    > }
    >
    > Thanks!
    >
    > Ken


    You may find this interesting:
    http://weblog.xanga.com/adam/598401947/another-c-template-programming-goodie.html

    I tried this adaption and works for me on g++ (GCC) 4.1.2 20070925
    (Red Hat 4.1.2-33)

    #include <iostream>
    using namespace std;

    template <typename T>
    class IsClass {
    private:
    typedef char One;
    typedef struct { char a[2]; } Two;
    template<typename C> static One test(int (C::*pmt));
    template<typename C> static Two test(...);
    public:
    enum { result = (sizeof(test<T>(0)) == 1 )};
    };

    class A {};

    int main()
    {
    if( !IsClass< int >::result )
    {
    cout << "int is not a class" << endl;
    }
    if( IsClass< A >::result )
    {
    cout << "A is a class" << endl;
    }
    }
    , Jul 19, 2008
    #4
  5. Re: Template function: Can it tell if parameter is primitive orclass?

    On Jul 18, 4:35 pm, wrote:
    > In C++, is there a way for atemplatefunctionto determine at runtimeifitstemplateparameteris aprimitivetype vs. aclass?
    >
    > I want to do something like this:
    >
    > template<typename T>
    > void doThis( T )
    > {
    >  if(T is aprimitivetype)
    >   {
    >      doOneThing();
    >   }
    >  else //it's aclass
    >   {
    >     doSomethingElse( );
    >   }
    >
    > }
    >
    > Thanks!
    >
    > Ken


    Perhaps you can use template specialization. I tried this and worked
    under VC++9.

    #include <iostream>

    using namespace std;

    class MyClass
    {
    };

    void doOneThing()
    {
    cout << "doOneThing" << endl;
    }

    void doSomethingElse()
    {
    cout << "doSomethingElse" << endl;
    }

    template<typename T>
    void doThis(T)
    {
    doSomethingElse();
    }

    template<>
    void doThis<int>(int)
    {
    doOneThing();
    }

    template<>
    void doThis<long>(long)
    {
    doOneThing();
    }

    int main()
    {
    int i = 0;
    long l = 0;
    MyClass myClass;

    doThis(i);
    doThis(l);
    doThis(myClass);

    return 0;
    }

    The output being:

    doOneThing
    doOneThing
    doSomethingElse

    Granted, you'd have to create a specialization for any primitive type
    you're interested. Anyway, it might be an option.

    Regards.
    Fernando Gómez, Jul 20, 2008
    #5
    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. case2005
    Replies:
    3
    Views:
    1,788
    Nicolas Pavlidis
    Feb 13, 2005
  2. christopher diggins
    Replies:
    16
    Views:
    729
    Pete Becker
    May 4, 2005
  3. Replies:
    7
    Views:
    590
    Victor Bazarov
    May 9, 2005
  4. Stuart Redmann
    Replies:
    5
    Views:
    466
    Stuart Redmann
    Dec 14, 2007
  5. Daniel Pitts
    Replies:
    7
    Views:
    460
Loading...

Share This Page