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