make install时有符号和无符号整数表达式之间的比较警告吗?

时间:2014-08-13 19:00:46

标签: c++ boost

我正在运行以下代码,用于检查data_timestamp是否超过两周。如果它超过两周,则打印hello否则打印world

#include <boost/chrono/chrono.hpp>
#include <iostream>

int main()
{
    uint64_t data_timestamp = 1406066507000;

    const boost::chrono::system_clock::time_point now = boost::chrono::system_clock::now();
    boost::chrono::hours twoWeeks = boost::chrono::hours(24 * 14);
    boost::chrono::system_clock::time_point lastTwoWeeks = now - twoWeeks;

    boost::int_least64_t millis = boost::chrono::duration_cast<boost::chrono::milliseconds>(lastTwoWeeks.time_since_epoch()).count();

    std::cout << "Time stamp in milliseconds since UNIX epoch start: "<< millis << std::endl;

    if (data_timestamp < millis) {
        std::cout << "Hello";
    } else {
        std::cout << "World";
    }

    return 0;
}

我上面的代码工作正常,但是如果语句比较有一个警告 -

if (data_timestamp < millis) {

我看到的警告是 -

warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

如何删除此警告?我希望将data_timestamp保留为uint64_t,因为在实际代码中它也会像其他地方一样使用。

1 个答案:

答案 0 :(得分:1)

您可以将millis投射到uint64_t。您遇到的问题是millis已签名,因此您可以通过将其转换为无符号类型来解决此问题。

if (data_timestamp < (uint64_t)millis) {
    std::cout << "Hello";
} else {
    std::cout << "World";
}