简体   繁体   English

这是Visual C ++ 2010中的错误,还是我错过了什么?

[英]Is this a bug in Visual C++ 2010, or am I missing something?

Given the following code: 给出以下代码:

#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());
}

When compiled with Visual C++ 2010 (SP1), I get the following errors: 使用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'

I don't understand them. 我不明白他们。 First, of course, the error message basically sums up as "There's something wrong here, but we won't tell you what'. And secondly, I don't find anything wrong; nor does g++ (version 4.4.2). Other interesting symptoms: if you add a using std::vector; after the include, and delete all of the std:: , it works—I would have thought that that should have no effect. And if you delete either the function f (which really isn't used anywhere) or the first version of function g , it also works. 首先,当然,错误信息基本上总结为“这里有问题,但我们不会告诉你什么。”其次,我没有发现任何错误; g ++(版本4.4.2)也没有。其他有趣的症状:如果你添加一个using std::vector;在include之后,并删除所有std:: ,它可以工作 - 我会认为那应该没有效果。如果你删除了函数f (其中)真的没有在任何地方使用)或功能g的第一个版本,它也有效。

So am I crazy, or is VC10 really not yet production-ready? 我疯了,还是VC10还没准备好生产?

EDITED: Just to add: if it is a bug in the compiler, how do I reliably work around it? 编辑:只是补充:如果它是编译器中的错误,我该如何可靠地解决它?

Indeed it appears a bug in the compiler. 实际上,它似乎是编译器中的一个错误。

In your simplified example, the issue goes away if the two versions of g() exchange places, or if f() is commented out, or f() exchange places with g<It,Op>(Op, It, It) . 在您的简化示例中,如果两个版本的g()交换位置,或者f()被注释掉,或f()g<It,Op>(Op, It, It)交换位置,问题就会消失。

It works fine with g++ compiler. 它适用于g ++编译器。 So it's possible that VC++ is not able to parse through it properly (may be a bug in that case). 因此VC ++可能无法正确解析它(在这种情况下可能是一个错误)。 Your code is proper. 你的代码是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM