Using templates can accomplish a lot of seemingly incredible things, such as the following, you can achieve static type judgment during compilation.
# Include <string>
# Include <iostream>
Template <typename T>
Class TypeChecker;
Template <> class TypeChecker <std: string>
{
Public:
Static const int TYPE = 1;
};
Template <> class TypeChecker <const char *>
{
Public:
Static const int TYPE = 2;
};
Template <> class TypeChecker <char *>
{
Public:
Static const int TYPE = 3;
};
Int main ()
{
Std: string dummy = "";
If (TypeChecker <typeof (dummy) >:: TYPE = 1)
{
Std: cout <"std: string" <std: endl;
}
Char * data = "";
If (! TypeChecker <typeof (data) >:: TYPE = 2)
{
Std: cout <"const char *" <std: endl;
}
}
From this we can see that the template basically defines all the situations you need, and then let the compiler choose which one applies.
From unintentional cloud