template <class T,class U>
class CConversion
{
typedef char Small;
class Big{char dumppy[2];};
static Small Test(U);
static Big Test(...);
static T MakeT();
public:
enum{exist=sizeof(Test(MakeT()))==sizeof(Small)};
};
#include "Conversion.h"
#include <iostream>
class testFather{};
class testChild:public testFather
{};
int main()
{
using namespace std;
cout<<CConversion<testChild,testFather>::exist<<endl;
}
sizeof会直接传回大小,不用等到执行期才评估。如果接受U的那个函数被调用,我们就知道T可转换成U。这边的重点是里面的自动转换。