Original Post
Still working on learning STL, I started working on a simple midrange function. It works correctly, but I get 2 C4244 warnings when I compile. testie2.cpp(27) : warning C4244: '=' : conversion from 'std::allocator<_Ty>::value_type' to 'float', possible loss of data with [ _Ty=int ] testie2.cpp(67) : see reference to function template instantiation 'T Midrange::iterator>(FwItr,FwItr)' being compiled with [ T=float, _Ty=int, FwItr=std::vector::iterator ] testie2.cpp(29) : warning C4244: '=' : conversion from 'std::allocator<_Ty>::value_type' to 'float', possible loss of data with [ _Ty=int ] Here is the function: It does correctly convert to a float, because the result sometimes ends with point five. So should I live with the warnings becase it still works? I am pretty sure there is a way to get these cleaned up. I do not get the warnings when I use a vector F with Midrange(...).
template <typename T, typename FwItr>
T Midrange( FwItr b, FwItr e )
{
T min = T(*b);
T max = T(*b);
for(; b != e; ++b)
{
if( *b > max )
max = *b; // line 27: C4244
if( *b < min )
min = *b; // line 29: C4244
}
return ( min + max ) / 2;
}
// V
vector<int> V;
// line 67: reference
cout << "Float Midrange: " << Midrange<float>(V.begin(), V.end()) << endl;