gcc 4.6编译器的奇怪行为

4
这段代码:
template<class Int_T, long long Min, unsigned long long Max>
class Int_Core
{
static_assert(Check_Range<Minimum>::check(Min,std::numeric_limits<Int_T>::min()),"INCORRECT Min range.");
static_assert(Check_Range<Maximum>::check(Max,std::numeric_limits<Int_T>::max()),"INCORRECT Max range.");
}

我得到的错误是第二个 static_assert 告诉我使用了非 const 表达式。但是,如果我在第二个断言中将 'Max' 改为 'Min',它就可以编译而没有问题。发生了什么事情?
错误:
error: non-constant condition for static assertion

这些是辅助类/函数:

//this is Int_Core.h file

struct Minimum
{/*eb*/};
struct Maximum
{/*eb*/};

/**Checks if given range is within boundary*/
template<class Range>
struct Check_Range;

template<>
struct Check_Range<Minimum>
{
template<class Value,class Limit>
static constexpr bool check(Value val,Limit limit)
{
    return greater_than_or_equal_with(val,limit);
}
};

template<>
struct Check_Range<Maximum>
{
template<class Value,class Limit>
static constexpr bool check(Value val,Limit limit)
{
    return greater_than_or_equal_with(val,limit);
}
};

constexpr bool greater_than(long long signed_,unsigned long long unsigned_)
{
//   unsigned long long mask = 0x8000000000000000LL;
//   bool is_negative = signed_ & mask;
//   if (is_negative)
//   {
//
//       return false;
//   }
//   else
//   {
//      return (signed_ > unsigned_);
//   }
//
   return (signed_ & 0x8000000000000000LL) ? false : (signed_ > unsigned_);
}

constexpr bool equal_with(long long signed_,unsigned long long unsigned_)
{
//   unsigned long long mask = 0x8000000000000000LL;
//   bool is_negative = signed_ & mask;
//   if (is_negative)
//   {
//
//       return false;
//   }
//   else
//   {
//      return (signed_ == unsigned_);
//   }
//This line is == to the commented code above (constexpr must have just return statement)
   return (signed_ & 0x8000000000000000LL) ? false : (signed_ == unsigned_);
}

constexpr bool greater_than_or_equal_with(long long signed_,unsigned long long unsigned_)
{
    return (greater_than(signed_,unsigned_) || equal_with(signed_,unsigned_));
}

更新:
#include "Int_Core.h"
    int main()
    {
       Int_Core<unsigned char,1,-50> a;
    }

1
你能提供任何测试用例吗? - Griwes
只是提醒一下,你的测试函数非常依赖于平台且不可移植。 - Kerrek SB
1
拥有一个类模板,可以根据实例化的情况充当有符号或无符号整数,这会增加复杂性。当你将这两种情况分开时,事情变得更简单了。也许你根本不需要无符号变量。 - Cheers and hth. - Alf
1
@KerrekSB 在一些边缘情况下,使用运算符<比较无符号和有符号数会失败。 - smallB
1
@KerrekSB,无论您是否使用type_traits,它(operator <)在极端情况下仍然无法正确运行。 - smallB
显示剩余10条评论
1个回答

4

这是gcc 4.6.0中的编译器错误,而gcc 4.6.1可以正确触发static_assert。


1
@smallB:当你想使用最新的功能/技术时,就是这样的。 - PlasmaHH
我可以接受这个 ;) 总有事情要做。 - smallB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接