2015-07-10 141 views
1

在VC++ 2013(和2015年RC)的名稱模板的說法,我覺得這會導致編譯錯誤:一類的另一個命名空間

namespace namespace1 
{ 
    template<typename T> 
    class Bar 
    {}; 
} 

namespace namespace2 
{ 
    template <unsigned Bar> 
    struct Foo 
    { 
     static const int value = (Bar < 1) ? 1 : 2; 
    }; 
} 

錯誤:

error C2059: syntax error : ')' 
: see reference to class template instantiation 'namespace2::Foo<Bar>' being compiled 
error C2143: syntax error : missing ')' before ';' 
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
error C2143: syntax error : missing ';' before ')' 
error C2059: syntax error : ')' 
error C2238: unexpected token(s) preceding ';' 
fatal error C1201: unable to continue after syntax error in class template definition 

如果我交換命名空間的順序,我不會收到錯誤。

爲什麼編譯器將Bar當作一個類型處理,當它不符合命名空間的限定時?

另外,如果我改變的值初始化到:

static const int value = (Bar > 1) ? 1 : 2; 

我沒有得到一個錯誤要麼。

遇到這樣在編譯谷歌協議緩衝,在這個結構的定義:

// Compile-time equivalent of VarintSize32(). 
template <unsigned Value> 
struct StaticVarintSize32 { 
    static const int value = 
     (Value < (1 << 7)) 
      ? 1 
      : (Value < (1 << 14)) 
       ? 2 
       : (Value < (1 << 21)) 
        ? 3 
        : (Value < (1 << 28)) 
         ? 4 
         : 5; 
}; 

不會因所謂的價值模板類存在於我們自己的代碼庫命名空間編譯。我現在通過確保首先包含相關的Procotol緩衝區頭文件來解決它,但似乎這是一個編譯器錯誤?

當我無法真正改變協議緩衝區代碼或Value類時,是否有其他解決方法?

+0

看起來像一個bug給我。 'Bar'類模板在使用'Bar'的地方不可見。作爲一種解決方法,您可以在'Bar'周圍添加一對額外的parens,如'static const int value =((Bar)<1)? 1:2;' –

+0

如上所述,這是在協議緩衝區代碼,雖然我可以改變它,但我不認爲這是正確的修復。 – Chris

回答

0

確認爲Microsoft的錯誤,將在2015 RTM中修復。

相關問題