gcc 4.6编译器的奇怪行为

时间:2011-10-14 07:22:46

标签: c++ gcc

此代码:

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',它确实编译没有问题。发生了什么事? 错误:
错误:静态断言的非常量条件

这些是辅助类/ fncs:

//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 个答案:

答案 0 :(得分:4)

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