2011-05-03 42 views
2
//THIS IS JUST A FRAGMENT OF A static_numeric_limits.h for the purpose of this example 
#include <limits.h> 

    template<class T> 
    struct static_numeric_limits; 

    template<> 
    struct static_numeric_limits<signed char> 
    {/*min was outside of range for enum*/ 
     static const signed char min = SCHAR_MIN, 
           max = SCHAR_MAX; 
    }; 

    /*This "surplus" template is here for the reason that char is threated differently from signed char */ 
    template<> 
    struct static_numeric_limits<char> 
    {/*min was outside of range for enum*/ 
     static const char min = SCHAR_MIN, 
          max = SCHAR_MAX; 
    }; 

    template<> 
    struct static_numeric_limits<unsigned char> 
    { 
     static const unsigned char min = 0x0, 
          max = UCHAR_MAX; 
    }; 
///REAL PROBLEM STARTS FROM HERE  
    template<class IntType,IntType low_range = static_numeric_limits<IntType>::min> 
    struct Int 
    { 
     Int():value_(IntType()) 
     {} 
     Int(const IntType& pattern) 
     { 
      value_ = (pattern); 
     } 
     constexpr inline IntType getValue()const 
     { 
      return value_; 
     } 
    private: 
     IntType value_; 
    }; 

    template<class IntType,class IntType_1> 
    auto operator+ 
     (Int<IntType>& lhs, Int<IntType_1>& rhs) 
     -> Int<decltype(lhs.getValue() + rhs.getValue())>//HERE IS THE PROBLEM 
    { 
     return lhs.getValue() + rhs.getValue(); 
    } 

錯誤(從VS2010) error C2027: use of undefined type 'static_numeric_limits<T>'
錯誤(GCC產生4.6)的另一個問題
error: 'decltype ((lhs->getValue() + rhs->getValue()))' is not a valid type for a template constant parameter與decltype

爲什麼不這項工作,因爲我認爲它會嗎?

+0

請注意,對於'getValue'標記爲'constexpr',您的構造函數需要標記爲'constexpr',這樣'value_'就有了一個常量表達式。 – GManNickG 2011-05-10 00:22:13

回答

1

這裏的錯誤是什麼類型decltype是從你的表達推論;不幸的是,錯誤信息並不清楚,實際上這是一個棘手的問題。考慮表達式的類型0 + 0。這是一個int,是的,但更重要的是它是一個右值(非正式的,這是一個臨時的)。這意味着decltype(0 + 0)不是int,而是int&&。現在認爲你的代碼沒有什麼不同,在這方面:你仍然有一個右值。

問題是模板非類型參數不能是右值引用,所以你不能有Int<int&&>,因爲第二個參數的類型。你可以做的,雖然是這樣的:

#include <type_traits> 

// ... 

template <class IntType, class IntType_1> 
auto operator+(const Int<IntType>& lhs, // be const-correct! 
       const Int<IntType_1>& rhs) 
       -> Int<typename std::remove_reference< 
         decltype(lhs.getValue() + rhs.getValue())>::type> 
{ 
    return lhs.getValue() + rhs.getValue(); 
} 

這爲基準掉int&&,讓你裸露int類型。希望gcc的錯誤信息更有意義:它試圖告訴你,你的非類型參數不能使用int&&


另一個問題,雖然可能一個不是問題的問題,是整數運算髮生什麼所謂的usual arithmetic conversions。因此,將兩個Int<char>的值相加的結果實際上將是int,因此您的返回類型應該是Int<int>(並且是固定代碼)。

問題在於你沒有定義static_numeric_limits<int>。但正如我所說,我懷疑這是一個非問題,你確實有它的定義,只是沒有顯示在你的問題。

+0

@GMan,這是夢幻般的答案。謝謝。你知道爲什麼這些降價嗎?這個問題出了什麼問題? – 2011-05-04 06:24:35

+0

@There:有一個很大的討論,但它被刪除了。人們覺得你發佈了一個咆哮,而不是一個問題,因爲你最後的陳述,所以關閉它,並投下它。我認爲情況並非如此,通過編輯進行了澄清,並建議人們只是決定將其視爲咆哮,因爲他們不喜歡其他問題。無論誰是對的,問題都重新打開,評論被刪除。儘管如此,儘量讓你的問題儘可能清楚,並儘量減少你的想法或挫折感。 – GManNickG 2011-05-04 06:37:24

+0

@GMan對不起,我的帖子的哪一部分顯示了一個咆哮?因爲上帝的愛不能表達個人的意見嗎?我以什麼方式侮辱或惹惱任何人?這很荒謬。 – 2011-05-04 07:09:03