具有:(不用擔心這個碼的長度,專注於結構X和範圍)(您可以複製和粘貼,並應編譯):
EDITEDstatic_assert安置問題
#include <limits.h>
#include <type_traits>
//This is from file "Static_limits.h"
template<class T>struct static_numeric_limits;
template<>struct static_numeric_limits<signed char>
{
enum {min = SCHAR_MIN,max = SCHAR_MAX};
};
/*This "surplus" template is here for the reason that char is threated diferently from signed char */
template<>struct static_numeric_limits<char>
{
enum {min = SCHAR_MIN,max = SCHAR_MAX};
};
template<>struct static_numeric_limits<unsigned char>
{
enum {min = 0x0,max = UCHAR_MAX};
};
template<>struct static_numeric_limits<unsigned short>
{
enum {min = 0x0,max = USHRT_MAX};
};
template<>struct static_numeric_limits<signed short>
{
enum {min = SHRT_MIN,max = SHRT_MAX};
};
template<>struct static_numeric_limits<unsigned int>
{
enum {min = 0x0,max = UINT_MAX};
};
template<>struct static_numeric_limits<signed int>
{
enum {min = INT_MIN,max = INT_MAX};
};
template<>struct static_numeric_limits<unsigned long>
{
enum {min = 0x0,max = ULONG_MAX};
};
template<>struct static_numeric_limits<signed long>
{
enum {min = LONG_MIN,max = LONG_MAX};
};
template<>struct static_numeric_limits<unsigned long long>
{
static const long long min = 0x0;
static const long long max = ULLONG_MAX;
};
template<>struct static_numeric_limits<signed long long>
{
#define LLONG_MAX 9223372036854775807LL
/* maximum signed long long int value */
static const long long min = LLONG_MIN;
static const long long max = LLONG_MAX;
};
//This is from main.cpp
typedef unsigned long long uint_64;
typedef signed long long int_64;
/*Validates range*/
template<class IntType, uint_64 value_,bool C = std::is_signed<IntType>::value>
struct validate_range;
template<class IntType,uint_64 value_>
struct validate_range<IntType,value_,true>
{
enum {value = (static_cast<int_64>(value_) >= static_numeric_limits<IntType>::min) &&
(static_cast<int_64>(value_) <= static_numeric_limits<IntType>::max)
};
};
template<class IntType,uint_64 value_>
struct validate_range<IntType,value_,false>
{
enum {value = (value_ >= static_numeric_limits<IntType>::min) &&
(value_ <= static_numeric_limits<IntType>::max)
};
};
template<class IntType, IntType value>
struct Range
{
private:
const IntType value_;
protected:
const IntType getRange()const
{
return value_;
} public:
Range():value_(value)
{
/*eb*/
}
//this static assert in here won't work even though this class is a base class for Low
static_assert((validate_range<IntType, value>::value),"Value constant is out of range");
};
template<class IntType, IntType value>
struct Low : private Range<IntType,value>//HERE Range IS INHERITED BY Low
{
const IntType getLowRange()const
{
return Range<IntType,value>::getRange();
}
};
template<typename IntType, uint_64 low_range>
struct X : public Low<IntType,low_range>
{};
//static_assert((validate_range<IntType, Value>::value),"Value constant is out of range");//this static doesn't work if placed in Low's base class namely Range };
int main(int argc, char** argv)
{
X<unsigned char, -2> x4;//this should fail
return 0;
}
所以基本上我在這裏問的是爲什麼static_assert((validate_range :: value),...如果放置在Range結構中不起作用,而這又是Low的基類,而這又是X的基類,但是如果放置直接在struct X EDITED
夥計們我很抱歉爲以前的代碼。我編輯了我的文章,這次它應該可以工作(揭示真正的問題)。再一次,對上一個代碼感到抱歉,我只是不知道發生了什麼。
當你說它「不起作用」時會發生什麼? – 2011-04-06 13:12:25
你至少需要一個'#include'在上面才能編譯。我仍然無法讓它在g ++上編譯(使用'-std = C++ 0x')。 'std :: is_signed '應該是'std :: numeric_limits :: is_signed',並且'low_range'被引用,但是沒有在任何地方定義。 –
2011-04-06 13:15:58
有幾個拼寫錯誤(可能?或者是缺少代碼,比如'low_range'),如果你用簡單的英文說明你想要達到的目的,這可能會有所幫助。 – 2011-04-06 13:20:22