Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

visual-c++

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:

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.

So am I crazy, or is VC10 really not yet production-ready?

EDITED: Just to add: if it is a bug in the compiler, how do I reliably work around it?

like image 337
James Kanze Avatar asked May 13 '11 08:05

James Kanze


1 Answers

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).

like image 153
Alexey Kukanov Avatar answered Oct 15 '22 06:10

Alexey Kukanov