64,686
社区成员
发帖
与我相关
我的任务
分享
#include <iostream> // std::cout
template<class T, class U>
class ConversionTo
{
typedef char Type1[1];
typedef char Type2[2];
static Type1& Test( U );
static Type2& Test(...);
static T MakeT();
public://1.为何仅声明未定义的函数名可以扔sizeof里去求大小2.为何sizeof可以对函数名求值?
enum { ret = sizeof(Test(MakeT()))==sizeof(Type1) };
};
int main()
{ //3.为什么这个类可以判断T类型是否可被转成U类型?
std::cout << ConversionTo<int, double>::ret << '\n';
std::cout << ConversionTo<float, int*>::ret << '\n';
std::cout << ConversionTo<const int&, int&>::ret << '\n';
std::cin.get(); return 0;
}