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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kk_oop@yahoo.com

    #1

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

    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<typena me T>
    void doThis( T )
    {
    if (T is a primitive type)
    {
    doOneThing();
    }
    else //it's a class
    {
    doSomethingElse ( );
    }

    }

    Thanks!

    Ken
  • Jerry Coffin

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

    In article <99048a55-20b7-4a25-9601-83a708efe286
    @f63g2000hsf.go oglegroups.com> , kk_oop@yahoo.co m 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<typena me 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 _characteristic s_ 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.

    Comment

    • James Kanze

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

      On Jul 18, 11:35 pm, kk_...@yahoo.co m 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<typena me 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:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      • sonison.james@gmail.com

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

        On Jul 19, 5:35 am, kk_...@yahoo.co m 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<typena me T>
        void doThis( T )
        {
          if (T is a primitive type)
          {
             doOneThing();
          }
         else //it's a class
          {
            doSomethingElse ( );
          }
        >
        }
        >
        Thanks!
        >
        Ken
        You may find this interesting:


        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<typena me Cstatic One test(int (C::*pmt));
        template<typena me Cstatic 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;
        }
        }

        Comment

        • =?ISO-8859-1?Q?Fernando_G=F3mez?=

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

          On Jul 18, 4:35 pm, kk_...@yahoo.co m wrote:
          In C++, is there a way for atemplatefuncti onto determine at runtimeifitstem plateparameteri s aprimitivetype vs. aclass?
          >
          I want to do something like this:
          >
          template<typena me 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 << "doSomethingEls e" << endl;
          }

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

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

          template<>
          void doThis<long>(lo ng)
          {
          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.

          Comment

          Working...