我怎样才能模仿一元 + 运算符正在做什么来让代码在 gcc-4.6 中正确编译?

How can I mimic what the unary + operator is doing to get code to compile properly in gcc-4.6?

我从 WebRTC 项目中借用了一些代码,我需要在具有 gcc-4.6 的系统上进行编译。代码在我的 gcc-4.8 环境下编译没有问题。在做了一些挖掘之后,看起来一元 + 在 gcc-4.6 和 gcc-4.8 上编译时可能不会做同样的事情。我这样说是因为枚举测试没有通过代码中的静态断言。

在后来的 GCC 机器上输出是:

$ g++ -std=c++0x test2.cpp -o test2
$ ./test2 
It's equal
It's not equal

但是在 gcc-4.6 机器上,编译会抛出静态断言错误:

$ g++ -std=c++0x test2.cpp -o test2                                                                                                                                                                       
test2.cpp:152:3: error: static assertion failed: ""                                                                                                                                                                                          
test2.cpp:153:3: error: static assertion failed: ""                                                                                                                                                                                          
test2.cpp:159:3: error: static assertion failed: ""                                                                                                                                                                                          
test2.cpp:160:3: error: static assertion failed: ""                                                                                                                                                                                          
test2.cpp: In function ‘int main()’:                                                                                                                                                                                                         
test2.cpp:195:15: error: no matching function for call to ‘Eq(int, int)’                                                                                                                                                                     
test2.cpp:195:15: note: candidates are:                                                                                                                                                                                                      
test2.cpp:185:2: note: template<class T1, class T2, typename std::enable_if<(safe_cmp_impl::IsIntlike<T1>::value && safe_cmp_impl::IsIntlike<T2>::value), void>::type* <anonymous> > bool Eq(T1, T2)                                         
test2.cpp:185:2: note: template<class T1, class T2, typename std::enable_if<((! safe_cmp_impl::IsIntlike<T1>::value) || (! safe_cmp_impl::IsIntlike<T2>::value)), void>::type* <anonymous> > bool Eq(T1&&, T2&&)                             
test2.cpp:197:15: error: no matching function for call to ‘Ne(int, int)’                                                                                                                                                                     
test2.cpp:197:15: note: candidates are:                                                                                                                                                                                                      
test2.cpp:186:2: note: template<class T1, class T2, typename std::enable_if<(safe_cmp_impl::IsIntlike<T1>::value && safe_cmp_impl::IsIntlike<T2>::value), void>::type* <anonymous> > bool Ne(T1, T2)                                         
test2.cpp:186:2: note: template<class T1, class T2, typename std::enable_if<((! safe_cmp_impl::IsIntlike<T1>::value) || (! safe_cmp_impl::IsIntlike<T2>::value)), void>::type* <anonymous> > bool Ne(T1&&, T2&&)

所以真正的问题是,我该怎么做才能让代码为 gcc-4.6 编译,同时保持函数的初衷。

代码似乎失败的部分:

    // Determines if the given type is an enum that converts implicitly to
    // an integral type.
    template <typename T>
    struct IsIntEnum {
        private:
            // This overload is used if the type is an enum, and unary plus
            // compiles and turns it into an integral type.
            template <typename X,
                 typename std::enable_if<
                     std::is_enum<X>::value &&
                     std::is_integral<decltype(+std::declval<X>())>::value>::type* =
                     nullptr>
                     static int Test(int);

            // Otherwise, this overload is used.
            template <typename>
                static char Test(...);

        public:
            static constexpr bool value =
                std::is_same<decltype(Test<typename std::remove_reference<T>::type>(0)),
                int>::value;
    };

// Determines if the given type is integral, or an enum that
// converts implicitly to an integral type.
template <typename T>
    struct IsIntlike {
        private:
            typedef typename std::remove_reference<T>::type X;

        public:
            static constexpr bool value =
                std::is_integral<X>::value || IsIntEnum<X>::value;
    };

namespace test_enum_intlike {

    enum E1 { e1 };
    enum { e2 };
    enum class E3 { e3 };
    struct S {};

    static_assert(IsIntEnum<E1>::value, "");
    static_assert(IsIntEnum<decltype(e2)>::value, "");
    static_assert(!IsIntEnum<E3>::value, "");
    static_assert(!IsIntEnum<int>::value, "");
    static_assert(!IsIntEnum<float>::value, "");
    static_assert(!IsIntEnum<S>::value, "");

    static_assert(IsIntlike<E1>::value, "");
    static_assert(IsIntlike<decltype(e2)>::value, "");
    static_assert(!IsIntlike<E3>::value, "");
    static_assert(IsIntlike<int>::value, "");
    static_assert(!IsIntlike<float>::value, "");
    static_assert(!IsIntlike<S>::value, "");

}  // test_enum_intlike

整个最小示例

#include <iostream>
#include <type_traits>

namespace safe_cmp_impl {

    template <size_t N>
        struct LargerIntImpl : std::false_type {};
    template <>
        struct LargerIntImpl<sizeof(int8_t)> : std::true_type {
            typedef uint16_t type;
        };
    template <>
        struct LargerIntImpl<sizeof(int16_t)> : std::true_type {
            typedef int32_t type;
        };
    template <>
        struct LargerIntImpl<sizeof(int32_t)> : std::true_type {
            typedef int64_t type;
        };

    // LargerInt<T1, T2>::value is true iff there's a signed type that's larger
    // than T1 (and no larger than the larger of T2 and int*, for performance
    // reasons); and if there is such a type, LargerInt<T1, T2>::type is an alias
    // for it.
    template <typename T1, typename T2>
        struct LargerInt
        : LargerIntImpl<sizeof(T1) < sizeof(T2) || sizeof(T1) < sizeof(int*)
          ? sizeof(T1)
          : 0> {};

    template <typename T>
        inline typename std::make_unsigned<T>::type MakeUnsigned(T a) {
            return static_cast<typename std::make_unsigned<T>::type>(a);
        }

    // Overload for when both T1 and T2 have the same signedness.
    template <typename Op,
         typename T1,
         typename T2,
         typename std::enable_if<std::is_signed<T1>::value ==
             std::is_signed<T2>::value>::type* = nullptr>
             inline bool Cmp(T1 a, T2 b) {
                 return Op::Op(a, b);
             }

    // Overload for signed - unsigned comparison that can be promoted to a bigger
    // signed type.
    template <typename Op,
         typename T1,
         typename T2,
         typename std::enable_if<std::is_signed<T1>::value &&
             std::is_unsigned<T2>::value &&
             LargerInt<T2, T1>::value>::type* = nullptr>
             inline bool Cmp(T1 a, T2 b) {
                 return Op::Op(a, static_cast<typename LargerInt<T2, T1>::type>(b));
             }

    // Overload for unsigned - signed comparison that can be promoted to a bigger
    // signed type.
    template <typename Op,
         typename T1,
         typename T2,
         typename std::enable_if<std::is_unsigned<T1>::value &&
             std::is_signed<T2>::value &&
             LargerInt<T1, T2>::value>::type* = nullptr>
             inline bool Cmp(T1 a, T2 b) {
                 return Op::Op(static_cast<typename LargerInt<T1, T2>::type>(a), b);
             }

    // Overload for signed - unsigned comparison that can't be promoted to a bigger
    // signed type.
    template <typename Op,
         typename T1,
         typename T2,
         typename std::enable_if<std::is_signed<T1>::value &&
             std::is_unsigned<T2>::value &&
             !LargerInt<T2, T1>::value>::type* = nullptr>
             inline bool Cmp(T1 a, T2 b) {
                 return a < 0 ? Op::Op(-1, 0) : Op::Op(safe_cmp_impl::MakeUnsigned(a), b);
             }

    // Overload for unsigned - signed comparison that can't be promoted to a bigger
    // signed type.
    template <typename Op,
         typename T1,
         typename T2,
         typename std::enable_if<std::is_unsigned<T1>::value &&
             std::is_signed<T2>::value &&
             !LargerInt<T1, T2>::value>::type* = nullptr>
             inline bool Cmp(T1 a, T2 b) {
                 return b < 0 ? Op::Op(0, -1) : Op::Op(a, safe_cmp_impl::MakeUnsigned(b));
             }

#define RTC_SAFECMP_MAKE_OP(name, op)      \
    struct name {                            \
        template <typename T1, typename T2>    \
        static constexpr bool Op(T1 a, T2 b) { \
            return a op b;                       \
        }                                      \
    };
    RTC_SAFECMP_MAKE_OP(EqOp, ==)
        RTC_SAFECMP_MAKE_OP(NeOp, !=)
        RTC_SAFECMP_MAKE_OP(LtOp, <)
        RTC_SAFECMP_MAKE_OP(LeOp, <=)
        RTC_SAFECMP_MAKE_OP(GtOp, >)
        RTC_SAFECMP_MAKE_OP(GeOp, >=)
#undef RTC_SAFECMP_MAKE_OP

        // Determines if the given type is an enum that converts implicitly to
        // an integral type.
        template <typename T>
        struct IsIntEnum {
            private:
                // This overload is used if the type is an enum, and unary plus
                // compiles and turns it into an integral type.
                template <typename X,
                     typename std::enable_if<
                         std::is_enum<X>::value &&
                         std::is_integral<decltype(+std::declval<X>())>::value>::type* =
                         nullptr>
                         static int Test(int);

                // Otherwise, this overload is used.
                template <typename>
                    static char Test(...);

            public:
                static constexpr bool value =
                    std::is_same<decltype(Test<typename std::remove_reference<T>::type>(0)),
                    int>::value;
        };

    // Determines if the given type is integral, or an enum that
    // converts implicitly to an integral type.
    template <typename T>
        struct IsIntlike {
            private:
                typedef typename std::remove_reference<T>::type X;

            public:
                static constexpr bool value =
                    std::is_integral<X>::value || IsIntEnum<X>::value;
        };

    namespace test_enum_intlike {

        enum E1 { e1 };
        enum { e2 };
        enum class E3 { e3 };
        struct S {};

        static_assert(IsIntEnum<E1>::value, "");
        static_assert(IsIntEnum<decltype(e2)>::value, "");
        static_assert(!IsIntEnum<E3>::value, "");
        static_assert(!IsIntEnum<int>::value, "");
        static_assert(!IsIntEnum<float>::value, "");
        static_assert(!IsIntEnum<S>::value, "");

        static_assert(IsIntlike<E1>::value, "");
        static_assert(IsIntlike<decltype(e2)>::value, "");
        static_assert(!IsIntlike<E3>::value, "");
        static_assert(IsIntlike<int>::value, "");
        static_assert(!IsIntlike<float>::value, "");
        static_assert(!IsIntlike<S>::value, "");

    }  // test_enum_intlike
}  // namespace safe_cmp_impl

#define RTC_SAFECMP_MAKE_FUN(name)                                      \
    template <typename T1, typename T2,                                   \
    typename std::enable_if<                                    \
    safe_cmp_impl::IsIntlike<T1>::value &&                  \
    safe_cmp_impl::IsIntlike<T2>::value>::type* = nullptr>  \
    inline bool name(T1 a, T2 b) {                                        \
        /* Unary plus here turns enums into real integral types. */         \
        return safe_cmp_impl::Cmp<safe_cmp_impl::name##Op>(+a, +b);         \
    }                                                                     \
    template <typename T1, typename T2,                                   \
    typename std::enable_if<                                    \
    !safe_cmp_impl::IsIntlike<T1>::value ||                 \
    !safe_cmp_impl::IsIntlike<T2>::value>::type* = nullptr> \
    inline bool name(T1&& a, T2&& b) {                                    \
        return safe_cmp_impl::name##Op::Op(a, b);                           \
    }
    RTC_SAFECMP_MAKE_FUN(Eq)
    RTC_SAFECMP_MAKE_FUN(Ne)
    RTC_SAFECMP_MAKE_FUN(Lt)
    RTC_SAFECMP_MAKE_FUN(Le)
    RTC_SAFECMP_MAKE_FUN(Gt)
RTC_SAFECMP_MAKE_FUN(Ge)
#undef RTC_SAFECMP_MAKE_FUN

int main()
{
    if( Eq( 1, 1 ) )
        std::cout << "It's equal" << std::endl;
    if( Ne( 1, 2 ) )
        std::cout << "It's not equal" << std::endl;

    return( 0 );
}

unary + 技巧可能会被 std::is_convertible<T, int>::value 取代。

此外,您编写 traits 的方式对于 gcc 4.6 来说是有问题的。

问题与默认模板参数有关。

转:

template <typename X,
          typename std::enable_if<
              std::is_enum<X>::value
              && std::is_integral<decltype(+std::declval<X>())>::value>::type* = nullptr>
static int Test(int);

进入

template <typename X>
static
typename std::enable_if<
                 std::is_enum<X>::value
                   && std::is_convertible<T, int>::value,
                 int>::type
Test(int);

Demo