2011-05-13 43 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()的交流场所。

相关问题