如何比较两个变量的类型是否相同
template <typename T1, typename T2>
inline const bool compare_type(T1, T2)
{
return false;
}
template <>
inline const bool compare_type(int, int)
{
return true;
}
template <>
inline const bool compare_type(float, float)
{
return true;
}
template <>
inline const bool compare_type(double, double)
{
return true;
}
template <>
inline const bool compare_type(char, char)
{
return true;
}
template <>
inline const bool compare_type(wchar_t, wchar_t)
{
return true;
}
template <typename T1, typename T2>
inline const bool compare_type(T1*, T2*)
{
return compare_type(T1(), T2());
}
template <typename T1, typename T2>
inline const bool compare_type(const T1*, const T2*)
{
return compare_type(T1(), T2());
}
template <typename T1, typename T2>
inline const bool compare_type(const T1*, T2*)
{
return false;
}
template <typename T1, typename T2>
inline const bool compare_type(T1*, const T2*)
{
return false;
}
通过特例化,我们可以很轻松的查看两个变量的类型是否相同。
inline const bool compare_type(T1, T2)
{
return false;
}
template <>
inline const bool compare_type(int, int)
{
return true;
}
template <>
inline const bool compare_type(float, float)
{
return true;
}
template <>
inline const bool compare_type(double, double)
{
return true;
}
template <>
inline const bool compare_type(char, char)
{
return true;
}
template <>
inline const bool compare_type(wchar_t, wchar_t)
{
return true;
}
template <typename T1, typename T2>
inline const bool compare_type(T1*, T2*)
{
return compare_type(T1(), T2());
}
template <typename T1, typename T2>
inline const bool compare_type(const T1*, const T2*)
{
return compare_type(T1(), T2());
}
template <typename T1, typename T2>
inline const bool compare_type(const T1*, T2*)
{
return false;
}
template <typename T1, typename T2>
inline const bool compare_type(T1*, const T2*)
{
return false;
}