2011-05-13 44 views
13

考慮下面的代碼:這是Visual C++ 2010中的一個錯誤,還是我錯過了一些東西?

#include <vector> 

template<class C1, class C2, class Op> 
std::vector<typename Op::result_type> 
f(Op op, const C1& src1, const C2& src2) 
{ 
} 

template<class It, class Op> 
std::vector<typename Op::result_type> g(Op op, It begin, It end) 
{ 
} 

template<class It1, class It2, class Op> 
std::vector<typename Op::result_type> g(Op op, It1 left_begin, It1 left_end, It2 right_begin) 
{ 
    return std::vector<typename Op::result_type>(); 
} 

struct ToS 
{ 
    typedef double result_type; 
    double operator() (long , double) const { return 0.0; } 
}; 

std::vector<double> h(std::vector<long> const& vl, std::vector<double> const& vd) 
{ 
    return g(ToS(), vl.begin(), vl.end(), vd.begin()); 
} 

當使用Visual C++ 2010(SP1)編譯,我得到以下錯誤:

1>VC10Error.cpp(30): error C2893: Failed to specialize function template 'std::vector<Op::result_type> g(Op,It1,It1,It2)' 
1>   With the following template arguments: 
1>   'std::_Vector_const_iterator<_Myvec>' 
1>   with 
1>   [ 
1>    _Myvec=std::_Vector_val<long,std::allocator<long>> 
1>   ] 
1>   'std::_Vector_const_iterator<_Myvec>' 
1>   with 
1>   [ 
1>    _Myvec=std::_Vector_val<double,std::allocator<double>> 
1>   ] 
1>   'ToS' 
1>VC10Error.cpp(30): error C2780: 'std::vector<Op::result_type> g(Op,It,It)' : expects 3 arguments - 4 provided 
1>   VC10Error.cpp(12) : see declaration of 'g' 

我不理解他們。首先,當然,錯誤信息基本上是 總結爲「這裏有問題,但我們不會告訴你是什麼」 其次,我沒有發現任何錯誤; g ++(版本4.4.2) 。 其他有趣的症狀:如果添加using std::vector;後 包括,並刪除所有std::的,它的工作原理—我會 認爲應該不會產生影響,如果你刪除要麼 功能f(這真的。沒有任何地方使用)或 功能g功能g的第一個版本,它也可以。

所以我瘋了,還是VC10真的還沒有生產閱讀ÿ?

編輯:只是要補充:如果它是編譯器中的錯誤,我該如何可靠地解決它?

+0

它們原本有更多的描述性名稱;我需要儘可能隱藏實際的代碼。原文中的類型等也較爲複雜;我試圖儘可能簡化。 –

+2

這*看起來像一個編譯器bug,移動第二個版本的'g',這樣它首先被聲明也似乎解決了這個問題。我建議你提交一個錯誤報告。 –

+0

James I通過connect.microsoft.com提交了一份關於此問題的錯誤報告。如果你想在VC++的下一個版本中看到它,請爲它投票。這裏是它的鏈接: https://connect.microsoft.com/VisualStudio/feedback/details/678280/vc-2010-sp1-compiler-fails-to-parse-conforming-code#details – 2011-07-04 13:37:55

回答

0

它適用於g ++編譯器。所以有可能VC++無法正確解析(可能是這種情況下的錯誤)。你的代碼是正確的。

3

事實上,它似乎是編譯器中的一個錯誤。

在您簡單的例子,問題消失如果g()交換位置的兩個版本,或者如果f()被註釋掉,或者g<It,Op>(Op, It, It)f()的交流場所。

相關問題