2011-04-06 2 views
0

은 (구조체의 X 및 범위에 집중,이 코드의 길이에 대해 걱정하지 마십시오) (복사 및 붙여 넣기 컴파일해야 할 수 있습니다) 데 :
그래서 기본적으로static_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에 배치되면 작동하지 않는다.이 구조체는 차례대로 X에 대한 기본 클래스이지만 배치 된 경우 작동하는 Low에 대한 기본 클래스입니다. 직접 구조체 X 수정 됨
얘들 아, 정말 미안해. 이전 코드의 경우 내 게시물을 편집하고 이번에는 실제로 작동해야합니다 (실제 문제 노출). 다시 한번, 마지막 코드에 대해 유감스럽게 생각합니다. 나는 무엇이 일어 났는지 정확히 모릅니다.

+3

"작동하지 않는다"고 말하면 어떻게됩니까? –

+2

이것을 컴파일하려면 적어도 #include 이 필요합니다. 나는 여전히 g ++에서 컴파일 할 수 없었다 ('-std = C++ 0x'). 'std :: is_signed '는'std :: numeric_limits :: is_signed'이어야하고'low_range'는 참조되지만 어디에도 정의되어 있지 않습니다. –

+3

몇 가지 오타가 있습니다 (아마도 'low_range'와 같은 코드가 누락되었거나 누락 된 것일 수 있습니다). 그리고 평범한 영어로 달성하고자하는 것을 언급하면 ​​도움이 될 것입니다. –

답변

1

다른 오류는 다음과 같습니다. 그들은 매우 자명 것 :

귀하의 최신 게시물 (LLONG_MAX을 다시 정의에 대한 경고는 제외) 나를 위해 컴파일

test.cpp:115:45: error: use of undeclared identifier 'Value'; did you mean 'value'? 
    static_assert((validate_range<IntType, Value>::value),"Value constant is out of range"); 
              ^~~~~ 
              value 
test.cpp:99:37: note: 'value' declared here 
    template<class IntType, IntType value> 
            ^
test.cpp:127:39: error: use of undeclared identifier 'low_range' 
     struct X : public Low<IntType,low_range> 
            ^
test.cpp:128:9: error: expected class name 
     { 
     ^
3 errors generated. 

편집.

+0

와우, 인상적인 오류 메시지 - "당신은 '가치'를 의미 했습니까?". :-) Clang 나는 추측한다. –

+0

맞습니까? clang. 그 팀은 품질 오류 메시지에 중점을 두었고 ** ** 실제로 보여줍니다! :-) –

+0

아,'error :','note :','^'는 색으로 구분됩니다! –