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

K

kk_oop

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
 
J

Jerry Coffin

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.
 
J

James Kanze

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.
 
S

sonison.james

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;
}
}
 
F

Fernando Gómez

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top