2012-05-22 46 views
11
struct X{}; 

template<class T> 
decltype(X() == int()) f(T const&){ return true; } 

int main(void) { 
    X x; 
    f(x); 
} 

爲什麼,只是爲什麼?沒有operator==定義任何地方爲什麼下面的代碼用MSVC++編譯?

我真的很想了解這裏發生了什麼,以提供有關MS Connect的詳細錯誤報告。我要瘋狂的旅程在休息室< C++>聊天室開始四處here ...

(注:無論GCC也不鏘接受這個代碼。)

哦,順便說一句和,加入私人X(int)構造函數使編譯失敗:

struct X{ 
    X(){} 
private: 
    X(int); 
}; 

template<class T> 
decltype(X() == int()) f(T const&){ return true; } 

int main(void) { 
    X x; 
    f(x); 
} 

輸出:

1>src\main.cpp(12): error C2248: 'X::X' : cannot access private member declared in class 'X' 
1>   src\main.cpp(4) : see declaration of 'X::X' 
1>   src\main.cpp(1) : see declaration of 'X' 
+0

其他編譯器接受它嗎? –

+0

@比利:沒有,補充說,信息。背景信息:我幾乎瘋了MSVC在C++聊天室中所做的事情,並在幾個小時後放棄了。 – Xeo

+0

添加了一些背景信息。另外,請匿名downvoter請說出downvote的原因? – Xeo

回答

7

什麼版本的MS VC++的一個你用嗎?

不管它可能是值得的,VC++ 11測試版會拒絕你的代碼:

trash.cpp(8): error C2893: Failed to specialize function template ''unknown-type' f(const T &)' 
      With the following template arguments: 
      'X' 

我不知道這就是我所稱之爲最有用的或錯誤提示信息,但它拒絕代碼。

在這種情況下,我猜想提交bug報告可能會完成很少(如果有的話)。我期待的迴應基本上是:「已經在VC++ 11中修復了,儘可能升級。」

+0

在這裏,我在想我實際上正在使用VS11測試版......你有什麼確切的版本?我的「版本11.0.40825.2 PREREL」。 – Xeo

+1

@ Xeo:Version 11.0.50214.1 BETAREL「如果我沒有弄錯,你有技術預覽版,而不是測試版。 –

+0

好吧,這麼多,下載*真實*測試版,稍後再回來檢查,謝謝 – Xeo