C++ 如何模仿一元数+;操作员正在做什么以使代码在gcc-4.6中正确编译?
我从WebRTC项目中借用了一些代码,需要在gcc-4.6系统上编译这些代码。代码在我的gcc-4.8环境中编译没有问题。在做了一点挖掘之后,看起来一元+在gcc-4.6和gcc-4.8上编译时可能做不到同样的事情。我这样说是因为enum测试没有通过代码中的静态断言 在以后的GCC机器上,输出为:C++ 如何模仿一元数+;操作员正在做什么以使代码在gcc-4.6中正确编译?,c++,gcc,compiler-errors,unary-operator,C++,Gcc,Compiler Errors,Unary Operator,我从WebRTC项目中借用了一些代码,需要在gcc-4.6系统上编译这些代码。代码在我的gcc-4.8环境中编译没有问题。在做了一点挖掘之后,看起来一元+在gcc-4.6和gcc-4.8上编译时可能做不到同样的事情。我这样说是因为enum测试没有通过代码中的静态断言 在以后的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
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&&)
$g++-std=c++0x test2.cpp-o test2
test2.cpp:152:3:错误:静态断言失败:“”
test2.cpp:153:3:错误:静态断言失败:“”
test2.cpp:159:3:错误:静态断言失败:“”
test2.cpp:160:3:错误:静态断言失败:“”
test2.cpp:在函数“int main()”中:
test2.cpp:195:15:错误:调用“Eq(int,int)”时没有匹配函数
测试2.cpp:195:15:注:候选人为:
测试2.cpp:185:2:注:模板布尔等式(T1,T2)
测试2.cpp:185:2:注:模板布尔等式(T1&,T2&)
test2.cpp:197:15:错误:对“Ne(int,int)”的调用没有匹配的函数
测试2.cpp:197:15:注:候选人为:
test2.cpp:186:2:注:模板bool-Ne(T1,T2)
test2.cpp:186:2:注:模板布尔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
//确定给定类型是否是隐式转换为
//整型的。
模板
结构强度{
私人:
//如果类型为枚举且为一元加号,则使用此重载
//编译并将其转换为整型。
模板::类型*=
nullptr>
静态int测试(int);
//否则,将使用此重载。
模板
静态炭试验(…);
公众:
静态constexpr布尔值=
std::is_same::value;
};
//确定给定类型是整数还是
//隐式转换为整型。
模板
类结构{
私人:
typedef typename std::remove_reference::type X;
公众:
静态constexpr布尔值=
std::is|u integral::value | IsIntEnum::value;
};
名称空间测试\u枚举\u intlike{
枚举E1{E1};
枚举{e2};
枚举类E3{E3};
结构S{};
静态断言(IsIntEnum::value,“”);
静态断言(IsIntEnum::value,“”);
静态断言(!IsIntEnum::value,“”);
静态断言(!IsIntEnum::value,“”);
静态断言(!IsIntEnum::value,“”);
静态断言(!IsIntEnum::value,“”);
静态断言(IsIntlike::value,“”);
静态断言(IsIntlike::value,“”);
静态断言(!IsIntlike::value,“”);
静态断言(IsIntlike::value,“”);
静态断言(!IsIntlike::value,“”);
静态断言(!IsIntlike::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 );
}
#包括
#包括
命名空间安全\u cmp\u impl{
模板
struct LargerIntImpl:std::false_type{};
模板
struct LargerIntImpl:std::true\u类型{
typedef uint16_t type;
};
模板
struct LargerIntImpl:std::true\u类型{
typedef int32_t type;
};
模板
struct LargerIntImpl:std::true\u类型{
typedef int64_t type;
};
//如果存在较大的有符号类型,则LargerInt::value为true
//大于T1(且不大于T2和int*中的较大者,用于性能
//如果存在这样的类型,则LargerInt::type是一个别名
//为了它。
模板
结构放大
:LargerIntImpl{};
模板
内联类型名称std::make_unsigned::type MakeUnsigned(TA){
返回站
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);