如何比较两个变量的类型是否相同

        template <typename T1, typename T2>
        inline const bool compare_type(T1, T2)
        {
            return false;
        }

        template <>
        inline const bool compare_type(intint)
        {
            return true;
        }

        template <>
        inline const bool compare_type(floatfloat)
        {
            return true;
        }

        template <>
        inline const bool compare_type(doubledouble)
        {
            return true;
        }

        template <>
        inline const bool compare_type(charchar)
        {
            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;
        }
通过特例化,我们可以很轻松的查看两个变量的类型是否相同。
posted @ 2012-08-25 16:14  lwch  Views(405)  Comments(0Edit  收藏  举报