7

I want to perform a narrowing conversion of a vector of doubles to a vector of floats. And I do not want Visual Studio throwing warning C4244, "possible loss of data", i.e. narrowing conversion.

Please do not post comments or answers saying I should not do this. I am aware of the consequences. I just want to suppress the warning. It is not acceptable to disable the warning outside the scope of this specific function, e.g. globally via project settings.

I also want to use either vector::copy or vector::assign. Avoiding the warning via transform() or for_each() with static_cast<> is.... just too explicit for my tastes. So this questions is how to disable the warning, not avoid it.

My attempt at warning suppression does not work:

vector<float> DoubleVectorToFloat( vector<double> & x ){
  #pragma warning( push )
  #pragma warning( disable : 4244 ) 
  return vector<float>( x.begin(), x.end() );
  #pragma warning( pop )  
}

I understand that disabling warnings is bad. But one size does not fit all. My library is real-time, and processes 10's MiB/s. I want template _Copy_unchecked1(etc) to be called; I do not want to pay the performance penalty of error checks.

1
  • Visual Studio allows disabling warning on a per file basis. That's more than a specific function but could be acceptable in your case.
    – Anon Mail
    Commented Oct 23, 2017 at 14:22

2 Answers 2

6

To disable this type of warning you may have to put this function into a module of its own and disable the warning at the top:

// top-of-file
#pragma warning( disable : 4244 ) 

// All your includes here

std::vector<float> DoubleVectorToFloat( std::vector<double> & x ){
  return std::vector<float>( x.begin(), x.end() );
}
// end-of-file
2
  • Thank you. This answers the question as posed. I agree with @bolov 's answer as the generally "correct" way to accomplish narrowing conversion of a vector, but that is not what I asked for. Commented Oct 24, 2017 at 7:00
  • I also agree with @bolov, but if someone else searches for this warning, then this may be the only solution.
    – quamrana
    Commented Oct 24, 2017 at 8:31
5

Use this instead:

std::vector<float> DoubleVectorToFloat(const std::vector<double>& x)
{
    std::vector<float> r;
    r.reserve(x.size());

    std::transform(x.begin(), x.end(), std::back_inserter(r),
                   [](double a) { return static_cast<float>(a); });
    return r;
}

std::transform is the correct way to transform a vector of a type to a vector of another type.

I am strongly suggesting this approach as opposed to using the vector constructor and silencing the warning.

3
  • I'm hoping someone will post an answer that disables the warning, rather than avoiding it. If not, I'll accept this answer. Commented Oct 23, 2017 at 14:34
  • @TysonHilmer have you verified the performance difference between your original method and this?
    – Caleth
    Commented Oct 23, 2017 at 14:48
  • 1
    @Caleth. For this specific function no. But the last time I profiled reserve() followed by copy( , , back_inserter()), it was significantly slower than insert(). So I am reluctant to use it. I just want simple implicit casting, without the complexity of extra code, and not to be told by Microsoft and the helpful folks on SO not to do this. I know what I'm doing. Commented Oct 23, 2017 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.