Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an explicit cast to suppress this warning?

Tags:

c++

stl

boost

The following code:

#include <cstdint>
#include <vector>
#include <boost/range/irange.hpp>

int main() {
    int64_t first = 0, last = 10;
    std::vector<double> result = boost::copy_range<std::vector<double>>(boost::irange(first, last));
}

generates the warning (and 100+ lines of templated call stack trace):

1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(600):
warning C4244: 'initializing' : conversion from 'unsigned __int64' to 'double', possible loss of data

I want to tell the compiler that I don't care that my int64_t are being converted to double. I also do not want to use a 32-bit int instead. I would usually use static_cast<double>(my64BitInt) to solve this, but that won't work for a range. Right now I'm resorting to compiler pragmas to suppress the warning but that's not ideal.

Edit: Here's a pastebin with the full compiler output.

like image 396
jcai Avatar asked Sep 27 '22 09:09

jcai


1 Answers

what Phil said;

However in this case, you could do much simpler using the iota algorithm:

#include <vector>
#include <boost/range/algorithm_ext.hpp>

int main() {
    std::vector<double> result(10);
    boost::iota(result, 0);
}
like image 154
sehe Avatar answered Oct 16 '22 00:10

sehe