I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so.
-edit-
I\'ll have several class
This article describes a neat trick: http://www.gotw.ca/publications/mxc++-item-4.htm
Here's the basic idea. You first need an IsDerivedFrom class (this provides runtime and compile-time checking):
template
class IsDerivedFrom
{
class No { };
class Yes { No no[3]; };
static Yes Test( B* ); // not defined
static No Test( ... ); // not defined
static void Constraints(D* p) { B* pb = p; pb = p; }
public:
enum { Is = sizeof(Test(static_cast(0))) == sizeof(Yes) };
IsDerivedFrom() { void(*p)(D*) = Constraints; }
};
Then your MyClass needs an implementation that's potentially specialized:
template
class MyClassImpl
{
// general case: T is not derived from SomeTag
};
template
class MyClassImpl
{
// T is derived from SomeTag
public:
typedef int isSpecialized;
};
and MyClass actually looks like:
template
class MyClass: public MyClassImpl::Is>
{
};
Then your main will be fine the way it is:
int main()
{
MyClass::isSpecialized test1; //ok
MyClass::isSpecialized test2; //ok also
return 0;
}