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.