64,637
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
template <typename T, typename U>
struct IsSameType
{
enum{value = false};
};
template <typename T>
struct IsSameType<T, T>
{
enum{value = true};
};
template <typename T, typename U>
bool IsSameValueType(T&, U&)//防止左值转换导致错误
{
return IsSameType<T, U>::value;
};
int main()
{
int i1;
int i2;
double d;
char ch;
int iarray[10];
int *p;
cout<<IsSameValueType(i1, i2)<<endl;
cout<<IsSameValueType(i1, ch)<<endl;
cout<<IsSameValueType(p, iarray)<<endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
template <typename T, typename U>
struct IsSameType
{
enum{value = false};
};
template <typename T>
struct IsSameType<T, T>
{
enum{value = true};
};
template <typename T, typename U>
bool IsSameValueType(T, U)
{
return IsSameType<T, U>::value;
};
int main()
{
int i1;
int i2;
double d;
char ch;
cout<<IsSameValueType(d, i2)<<endl;
system("pause");
return 0;
}
template<typename T> bool same(T, T) { return true; }
template<typename T, typename U> bool same(T, U){return false;}
int main()
{
int arr[7];
int* ptr;
cout << same(arr, ptr) << endl;
return 0;
}
template<typename T1, typename T2>
int same_type(const T1&, const T2&)
{
return 0;
}
template<typename T1>
int same_type(const T1&, const T1&)
{
return 1;
}
int main()
{
int i;
char ch1;
char ch2;
cout << same_type(i, ch1) << endl;
cout << same_type(ch1, ch2) << endl;
return 0;
}